che
che

Reputation: 1

Getting a file name from directory in Lua

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

Answers (2)

BMitch
BMitch

Reputation: 265190

See this entry on lua-list

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

drio
drio

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

Related Questions