Get recursive files - A small script

Hello, i made a small script, that allow you to search for files in Subfolder too.
It return a table with all paths and filenames in the given folder and subfolders.
Very usefull for resource.AddFile()
file.GetRecrusiveFiles(string,table(optional))
For Example: file.GetRecursiveFiles("…/models/",{".mdl",".phy"})

models/alyx_animations.mdl
models/player/group03/male_08.mdl
models/player/group03/male_08.phy

Have Fun
[LUA]function file.GetRecursiveFiles(dir,filter)
local tbl = {}
local files = file.Find(dir…"")
local folders = file.FindDir(dir…"/
")
local function DoFilter(filename)
if !filter then return true end
for k,v in pairs(filter) do
local len = string.len(v)
local extension = string.Right(filename,len)
if v == extension then return true end
end
return false
end
local function IsFolder(v)
if !folders then return false end
if table.HasValue(folders,v) then return true end
return false
end
if files then
for k,v in pairs(files) do
local str = string.lower(string.Right(v,5))
if str != “.ztmp” and !IsFolder(v) and DoFilter(v) then
local path = string.Replace(dir,"…/","")…v
table.insert(tbl,path)
end
end
end
if folders then
for k,folddir in pairs(folders) do
local filetbl = file.GetRecursiveFiles(dir…folddir…"/",filter)
for _,found in pairs(filetbl) do
table.insert(tbl,found)
end
end
end
return tbl
end
[/LUA]

it’s spelled “recursive”

you should move those local functions out of the main function for more slightly efficiency and much more readability

Shouldn’t “GetRecrusiveFiles” be “GetRecursiveFiles”?

ninja’d

lol who rated me late?

EDIT: funny people

EDIT2: you could post that here:
http://www.facepunch.com/showthread.php?t=786778
it would make a nice addition to utilx

Ok, i fixed the code :smiley:

I just noticed you made your own IsFolder function
there is file.IsDir, why don’t you use that?

DrTight, I noticed you seem to use ULX from another thread, which would also mean you use ULib.
Your release is great for those who don’t use ULib, but you could use ULib’s filesInDir command personally if you wished.
You can specify whether or not it search recursively. (Working in SVN only as of this post)

Oh, never seen this xD

I’m not sure why you were rated dumb DrTight.
Many don’t know about the variety of functions within Ulysses Library. That’s not dumb… it’s just unknown, and many don’t know to ask or where to look.

Your code would be good I guess for the UtilX project.
It seems to be trying to bring together what several other Gmod based Lua libraries have done, but, is trying to be more community supported.