Add all files inside a folder with resource.AddFile?

I got like almost 200 files to add and make my clients download, mostly is just small material files but still 200 files…
Is there any way to like add all the files inside a folder instead of adding all of those resource.AddFile(“materials/lulz.vtf”) ?

[lua]function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
local list = file.FindDir("…/"…dir…"/*")
for _, fdir in pairs(list) do
if fdir != “.svn” then // don’t spam people with useless .svn folders
AddDir(dir…"/"…fdir)
end
end

for k,v in pairs(file.Find("../"..dir.."/*")) do
	resource.AddFile(dir.."/"..v)
end

end[/lua]

Did you even try searching?

Yes I did, I came up with the exactly same Code but I didn’t get how to use it, I am no lua coder.
I tried to do this.

function resource.AddDir(“materials/models/link”)
for k,v in pairs(file.Find(“materials/models/link/*”)) do
if !file.IsDir(“materials/models/link”) then
resource.AddSingleFile(“materials/models/link”)
end
end
end

That completely defeats the purpose of it being a function.

AddDir(“materials/models/link”) is all you need.

Ok. Thank you so much.

[editline]12th July 2011[/editline]

Btw did not work

Ugh for the love of god, okay. Here is what you do.

in the server autorun folder make a lua file called “Stupidity.lua”

Very important it is named that, and then inside it put …

[lua]
function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
local list = file.FindDir("…/"…dir…"/*")
for _, fdir in pairs(list) do
if fdir != “.svn” then // don’t spam people with useless .svn folders
AddDir(dir…"/"…fdir)
end
end

for k,v in pairs(file.Find("../"..dir.."/*")) do
    resource.AddFile(dir.."/"..v)
end

end
[/lua]

** BELOW THAT CODE** put this
[lua]
AddDir( “TheDirectoryYouWantToAdd” )
[/lua]

and incase you didn’t understand the 2nd part, you replace the text “TheDirectoryYouWantToAdd” with the file folder you are trying to add.

To idiot-proof what LauScript is saying even more, you’ll want to make sure that when specifying the directory for AddDir you do it like this path/to/directory with no trailing forward slash.

e.g:

[lua]
AddDir(“path/to/directory”);
AddDir(“materials/models/someshit”);
[/lua]

[lua] function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
local list = file.FindDir("…/"…dir…"/*")
for _, fdir in pairs(list) do
if fdir != “.svn” then // don’t spam people with useless .svn folders
AddDir(dir…"/"…fdir)
end
end

for k,v in pairs(file.Find("../"..dir.."/*")) do
	resource.AddFile(dir.."/"..v)
end

end

AddDir(“models/yourmodels”) [/lua]

Doing all of those resource.Addfile lines of code is tedious. Use the code above ^^^ speed things up a bit.

Sorry for bumping such an extremely old thread, but for anyone who ran into this searching for how to accomplish this task, I found (although it might just be me) that the methods explained above do not work.

If you come to a similar conclusion, I would recommend using Fox Warrior’s Resource Generator to generate a download file to be placed into lua/autorun/server from your garrysmod directory.

Make sure you add necessary extensions in the resource generator if the files aren’t being recognized. If you’re having trouble with how to get them into your server for clients to download, once it selects all the necessary files, use File/Generate lua file/Clipboard/AddSingleFile and it will copy the necessary code to your clipboard. Then paste it into a .lua file (the name doesn’t matter) in the lua/autorun/server folder.

Hope this was helpful!

a few functions changed in the file library, this is what i currently use:
[lua]
function addContentFolder( path )
local files, folders = file.Find( path … “/*”, “GAME” )
for k, v in pairs( files ) do
resource.AddFile( path … “/” … v )
end

    for k, v in pairs( folders ) do
            addContent( path .. "/" .. v )
    end

end

–Example:
addContentFolder( “materials/test” )
[/lua]
It adds the folder recursively (i.e. it adds the folder and all folders inside of it).