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()
For Example: file.GetRecursiveFiles("…/models/",{".mdl",".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
return false
local function IsFolder(v)
if !folders then return false end
if table.HasValue(folders,v) then return true end
return false
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
if folders then
for k,folddir in pairs(folders) do
local filetbl = file.GetRecursiveFiles(dir…folddir…"/",filter)
for _,found in pairs(filetbl) do
return tbl

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”?


lol who rated me late?

EDIT: funny people

EDIT2: you could post that here:
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.