Reputation: 1
I need to get the file name from a directory in a Lua.
I don't want to use require "lfs"
.
Using popen / open would be helpful.
Upvotes: 0
Views: 3076
Reputation: 265190
Specifically the following can be modified to do what you want:
local dircmd = "find . -type f -print" -- default to Unix
if string.sub(package.config,1,1) == '\\' then
-- Windows
dircmd = "dir /b/s"
end
os.execute(dircmd .. " > zzfiles")
local luafiles = {}
for f in io.lines("zzfiles") do
if f:sub(-4) == ".lua" then
luafiles[#luafiles+1] = f
end
end
print(table.concat(luafiles, "\n"))
Upvotes: 4
Reputation: 51
Use the shell function in the lua wiki. As a command (c) pass a "ls /path/pattern" (assuming you are in unix or have cygwin installed if running windows).
Upvotes: 0