如何在Lua中获取目录列表 [英] How to get list of directories in Lua

查看:237
本文介绍了如何在Lua中获取目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要LUA中的目录列表

I need a list of directory in LUA

假设我的目录路径为"C:\ Program Files"

Suppose I have a directory path as "C:\Program Files"

我需要该特定路径中所有文件夹的列表以及如何在该列表中搜索任何特定文件夹.

I need a list of all the folders in that particular path and how to search any particular folder in that list.

示例

需要路径"C:\ Program Files"中所有文件夹的列表

Need a list of all the folder in path "C:\Program Files"

下面是上面路径中的文件夹名称

Below are folder name in the above path

  1. test123
  2. test4567
  3. 123文件夹
  4. 456文件夹
  5. 文件夹456789

  1. test123
  2. test4567
  3. folder 123
  4. folder 456
  5. folder 456 789

需要在列表中获得以上内容,然后仅在文件夹456 789中搜索特定字符串,例如文件夹456.

Need to get the above in a list and then have to search for a particular string like folder 456 in folder 456 789 only.

尝试过以下代码.我在下面缺少的东西:-

Have Tried below code. Something I am missing below:-

local function Loc_Lines( str )
--
local ret= {}   -- 0 lines

while str do
    local _,_,line,tail= string.find( str, "(.-)\n(.+)" )
    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 ) == '\n' 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\\")

推荐答案

以简单的方式安装lfs.然后使用以下结构来查找所需的内容:

Take the easy way, install lfs. Then use the following constructs to find what you need:

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

请注意,反斜杠等于[[\]]等于"\\",并且如果未在cmd本身上使用反斜杠,也可以在Windows/中使用(如果我错了,请纠正我).

notice that a backslash equals [[\]] equals "\\", and that in windows / is also allowed if not used on the cmd itself (correct me if I'm wrong on this one).

这篇关于如何在Lua中获取目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆