Jump to content




List all Files of a Computer


  • You cannot reply to this topic
2 replies to this topic

#1 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 28 August 2016 - 10:47 AM

How can I get a Table with all files on a Computer? fs.list shows only one directory. The files should have the corect path, so that I can work with them.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 August 2016 - 12:21 PM

Use fs.list recursively.

local tFiles = {}
local scanDir
function scanDir( sDir )
  local t = fs.list( sDir ) 
  for k, v in pairs( t ) do
    local sFullName = fs.combine( sDir, v )
    if fs.isDir( sFullName ) then
      scanDir( sFullName )
    else
      tFiles[ #tFiles + 1 ] = sFullName
    end
  end
end


#3 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 28 August 2016 - 01:01 PM

View PostKingofGamesYami, on 28 August 2016 - 12:21 PM, said:

Use fs.list recursively.
Whilst this will work, it's a bad practice to use functions recursively, since it can cause stack overflow exceptions.
Use a stack:
local files = { }
local stack = { "" }
while #stack > 0 do
    local dir = stack[1]
    table.remove(stack, 1)
    local t = fs.list(dir)
    for i = 1, #t do
        local path = dir.."/"..t[i]
        if fs.isDir(path) then
            table.insert(stack, 1, path)
        else
            files[#files + 1] = path
        end
    end
end

Note: this will also include all files in /rom.
If you want to ignore the /rom directory, use this:
local files = { }
local stack = { "" }
while #stack > 0 do
    local dir = stack[1]
    table.remove(stack, 1)
    if dir ~= "/rom" then
        local t = fs.list(dir)
        for i = 1, #t do
            local path = dir.."/"..t[i]
            if fs.isDir(path) then
                table.insert(stack, 1, path)
            else
                files[#files + 1] = path
            end
        end
    end
end

Edited by CrazedProgrammer, 28 August 2016 - 01:04 PM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users