How to get list of directories in Lua
我需要LUA中的目录列表
假设我的目录路径为" C:\\\\ Program Files"
我需要该特定路径中所有文件夹的列表以及如何在该列表中搜索任何特定文件夹。
示例
需要路径" C:\\\\ Program Files"中所有文件夹的列表。
下面是上面路径中的文件夹名称
test123
test4567
文件夹123
文件夹456
文件夹456789
需要在列表中获得以上内容,然后仅在文件夹456 789中搜索特定的字符串,例如文件夹456。
尝试过以下代码。我在下面缺少的东西:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | local function Loc_Lines( str ) -- local ret= {} -- 0 lines while str do local _,_,line,tail= string.find( str,"(.-)\ (.+)" ) table.insert( ret, line or str ) str= tail Print (str) end return ret end local function Loc_ShellCommand( cmd ) -- local str= nil -- local f= io.popen( cmd ) -- no command still returns a handle :( if f then str= f:read'*a' Print(str) f:close() end if str=="" then -- take no output as a failure (we can't tell..) Print("hi") str= nil end -- Remove terminating linefeed, if any (eases up one-line analysis) -- if str then if string.sub( str, -1 ) == '\ ' then str= string.sub( str, 1, -2 ) end end return str end local function Loc_DirCmd( cmd ) Print(cmd) local str= Loc_ShellCommand( cmd ) return Loc_Lines(str) end local function Loc_DirList( dirname ) local ret= {} local lookup= {} local tbl= Loc_DirCmd("dir /AD /B"..dirname ) -- only dirs -- Add slash to every dir line -- for i,v in ipairs(tbl) do table.insert( ret, v..'\\\' ) lookup[v]= true end -- Return with forward slashes -- if true then for i=1,table.getn(ret) do ret[i]= string.gsub( ret[i], '\\\', '/' ) Print (ret[i]) end end return ret end Loc_DirList("C:\\\\Program Files") |
我讨厌必须安装库(尤其是那些希望我使用安装程序包来安装它们的库)。如果您正在为Lua中的绝对路径上的目录列表寻找一种干净的解决方案,请不要再犹豫了。
基于sylvanaar提供的答案,我创建了一个函数,该函数返回给定目录的所有文件的数组(需要绝对路径)。这是我的首选实现,因为它可以在我的所有机器上使用。
1 2 3 4 5 6 7 8 9 10 11 | -- Lua implementation of PHP scandir function function scandir(directory) local i, t, popen = 0, {}, io.popen local pfile = popen('ls -a"'..directory..'"') for filename in pfile:lines() do i = i + 1 t[i] = filename end pfile:close() return t end |
如果您使用的是Windows,则需要安装bash客户端,这样\\'ls \\'命令才能起作用-或者,您可以使用sylvanaar提供的dir命令:
1 | 'dir"'..directory..'" /b /ad' |
采用简单的方法,安装lfs。然后使用以下构造找到所需的内容:
1 2 3 4 5 6 7 8 9 | require'lfs' for file in lfs.dir[[C:\\Program Files]] do if lfs.attributes(file,"mode") =="file" then print("found file,"..file) elseif lfs.attributes(file,"mode")=="directory" then print("found dir,"..file," containing:") for l in lfs.dir("C:\\\\Program Files\"..file) do print("",l) end end end |
请注意,反斜杠等于
1 | for dir in io.popen([[dir"C:\\Program Files" /b /ad]]):lines() do print(dir) end |
*对于Windows
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Adobe Bitcasa Bonjour Business Objects Common Files DVD Maker IIS Internet Explorer iPod iTunes Java Microsoft Device Emulator Microsoft Help Viewer Microsoft IntelliPoint Microsoft IntelliType Pro Microsoft Office Microsoft SDKs Microsoft Security Client Microsoft SQL Server Microsoft SQL Server Compact Edition Microsoft Sync Framework Microsoft Synchronization Services Microsoft Visual Studio 10.0 Microsoft Visual Studio 9.0 Microsoft.NET MSBuild ... |
每次循环时,都会为您提供一个新的文件夹名称。我选择将其打印为示例。
我也不喜欢安装库,而是在嵌入式设备上工作,而内存比计算机少。我发现使用\\'ls \\'命令会导致内存不足。因此,我创建了一个使用\\'find \\'来解决问题的函数。
这样可以保持内存使用率稳定并循环所有30k文件。
1 2 3 4 5 6 | function dirLookup(dir) local p = io.popen('find"'..dir..'" -type f') --Open directory look for files, save data in p. By giving '-type f' as parameter, it returns all files. for file in p:lines() do --Loop through all files print(file) end end |
您还可以安装和使用\\'paths \\'模块。然后,您可以轻松地执行以下操作:
1 2 3 4 5 6 7 8 9 10 11 | require 'paths' currentPath = paths.cwd() -- Current working directory folderNames = {} for folderName in paths.files(currentPath) do if folderName:find('$') then table.insert(folderNames, paths.concat(currentPath, folderName)) end end print (folderNames) |
-这将打印所有文件夹名称
(可选)您还可以通过将
来查找具有特定扩展名的文件名
如果您正在基于Unix的计算机上运行,??则可以使用以下代码获取文件的数字排序列表:
1 2 3 4 5 6 | thePath = '/home/Your_Directory' local handle = assert(io.popen('ls -1v ' .. thePath)) local allFileNames = string.split(assert(handle:read('*a')), '\ ') print (allFileNames[1]) -- This will print the first file name |
第二个代码还排除诸如\\'。\\'和\\'.. \\'之类的文件。所以去很高兴!
IIRC,库存Lua无法获得目录列表。您需要自己编写一些粘合代码,或使用LuaFileSystem。后者很可能是您阻力最小的途径。快速浏览文档会显示
还原为Monica的解决方案说,很少有val修复程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function scandir(directory) local pfile = assert(io.popen(("find '%s' -mindepth 1 -maxdepth 1 -type d -printf '%%f\\\\0'"):format(directory), 'r')) local list = pfile:read('*a') pfile:close() local folders = {} for filename in string.gmatch(list, '[^%z]+') do table.insert(folders, filename) end return folders end |
现在它按文件夹过滤,不包含目录本身,仅显示名称。
不要解析
1 2 3 4 5 6 7 8 9 10 11 | function scandir(directory) local i, t = 0, {} local pfile = assert(io.popen(("find '%s' -maxdepth 1 -print0"):format(directory), 'r')) local list = pfile:read('*a') pfile:close() for filename in s:gmatch('[^\\0]+') i = i + 1 t[i] = filename end return t end |
警告:但是,如果目录名称中包含