List files in a directory
9 replies



02.12.21 05:58:25 pm
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?
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
In Windows, this is easy with io.popen
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
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
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
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
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


2084729947754920835
Admin/mod comment:

https://ohaz.engineer - Software Engineering

Oh, woah!
Is it any faster though?
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.


Code:
Here are some functions I have grabbed from my framework. Feel free to use them. 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
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
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
Shit your pants:
Outlast II Mod (29) | Create your UI faster: CS2D UI Framework





