Forum

> > CS2D > Scripts > List files in a directory
Forums overviewCS2D overview Scripts overviewLog in to reply

English List files in a directory

9 replies
To the start Previous 1 Next To the start

old List files in a directory

Mami Tomoe
User Off Offline

Quote
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?

old Re: List files in a directory

MikuAuahDark
User Off Offline

Quote
In Windows, this is easy with io.popen
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.
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

old Re: List files in a directory

Masea
Super User Off Offline

Quote
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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview