How to get list of directories in Lua

前端 未结 8 1906
离开以前
离开以前 2020-12-01 03:46

I need a list of directory in LUA

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

I need a list of all the folders in that particular path and

相关标签:
8条回答
  • 2020-12-01 04:04

    You also install and use the 'paths' module. Then you can easily do this as follow:

    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)
    

    -- This will print all folder names

    Optionally, you can also look for file names with a specific extension by replacing fileName:find('$') with fileName:find('txt' .. '$')

    If you're running on a Unix-based machine you can get a numerically-sorted list of files using the following code:

    thePath = '/home/Your_Directory'
    local handle = assert(io.popen('ls -1v ' .. thePath)) 
    local allFileNames = string.split(assert(handle:read('*a')), '\n')
    
    print (allFileNames[1]) -- This will print the first file name
    

    The second code also excludes files such as '.' and '..'. So it's good to go!

    0 讨论(0)
  • 2020-12-01 04:05

    I don't like installing libraries either and am working on an embedded device with less memory power then a pc. I found out that using 'ls' command lead to an out of memory. So I created a function that uses 'find' to solve the problem.

    This way it was possible to keep memory usage steady and loop all the 30k files.

    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
    
    0 讨论(0)
  • 2020-12-01 04:08

    Few fixes of val says Reinstate Monica solution:

    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
    

    Now it filters by folders, excludes dir itself and prints only names.

    0 讨论(0)
  • 2020-12-01 04:17
     for dir in io.popen([[dir "C:\Program Files\" /b /ad]]):lines() do print(dir) end
    

    *For Windows

    Outputs:

    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
    ...
    

    Each time through the loop you are given a new folder name. I chose to print it as an example.

    0 讨论(0)
  • 2020-12-01 04:17

    IIRC, getting the directory listing isn't possible with stock Lua. You need to write some glue code yourself, or use LuaFileSystem. The latter is most likely the path of least resistance for you. A quick scan of the docs shows lfs.dir() which will provide you with an iterator you can use to get the directories you are looking for. At that point, you can then do your string comparison to get the specific directories you need.

    0 讨论(0)
  • 2020-12-01 04:26

    Don't parse ls, it's evil! Use find with zero-terminated strings instead (on linux):

    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
    

    WARNING: however, as an acceped answer this apporach could be exploited if directory name contain ' in it. Only one safe solution is to use lfs or other special library.

    0 讨论(0)
提交回复
热议问题