lua遍历目录下所有文件

--递归遍历目录,传入func参数格式固定
function checkBackup(filedir)
	if(not filedir) then
		print('A Null Value!')
		return nil
	end
	if(string.find(filedir,'/')~=1) then
		filedir=Path..filedir
	end	

	local filenum=0
	function traver(rootPath)
		local traverpath,attr
		for entry in lfs.dir(rootPath) do
			if entry~='.' and entry~='..' then
				traverpath = rootPath.."/"..entry
				attr = lfs.attributes(traverpath)
				if(type(attr)~="table") then --如果获取不到属性表则报错
					print('ERROR:'..traverpath..'is not a path')
					save:close()
					return nil
				end
				if(attr.mode == "directory") then
					traver(traverpath)
				elseif attr.mode=="file" then
					filenum=filenum+1
					--处理函数
					dosomething(traverpath)					
				end
			end
		end
	end
	if(not traver(filedir)) then
		return nil
	end

	return true
end

你可能感兴趣的:(lua,unix)