How do I loop through a directory?

I’ve set up a server and made a fastdl however I don’t want to make a lua file with every file in the /models/ I’ve thought about looping through the directory and having a addFile(FileName) or something like that but the thing is I don’t know how to, the fastdl is on a normal HTTP server with no index file but Apache shows the directory.

We’ve had a few clients connect and say that the models are nothing but errors, the sounds finally work after a long list of addFile (before we set up a fastdl)

We’ve also tried making it via a steam workshop collection with our addons and textures but this doesn’t seem to work.

I’ve added more details maybe more than needed, if you need anything please tell me.

Adapt these how you will.



function LoadDirectory(dir)
	print("==[LOADING "..dir.."]===========================================")
	local list = file.Find("gamemodes/garry/gamemode/"..dir.."/*.lua", "GAME") /** Change *.lua to what file format you want to load for example .mdl **/
		for _, f in pairs(list) do
			local directory = dir.."/"..f
			resource.AddFile(directory)	
			print("    >Loaded "..directory)
		end
		print("    >Loaded the directory "..dir)
end


Change the file.Find path to the root folder which holds the folder models
Then call it using:-



LoadDirectory("models") 


There is also the dir function afaik.
Not sure if it’s in glua.

It’s a Lua module.

Right, that would make more sense. It’s been a while since I used it.

THANK YOU!!! I knew it was something like this but I wasn’t sure if GMod would work like that.

[editline]8th March 2014[/editline]

Is there a way to make it loop through all the folders in /addons/ for a .mdl? I know how it works per file, I’m trying to put it in the


/garrysmod/lua/autorun/server

so the server runs it on start up.

I’m not sure if /addons can be accessed from gmod itself, given that everything in it gets merged to a virtual game directory.

It can be, and if you’re worried about mounting (which you really shouldn’t be) then you can just use MOD instead of GAME. Here’s the function I made for including files, but changed for what you want:


function LoadDir( workingDir )

	local files, dirs = file.Find( workingDir .. "/*.mdl", "MOD" )

	for k, v in pairs( files ) do
		resource.AddFile( workingDir .. "/" .. v )
	end

	for k, v in pairs( dirs ) do
		LoadDir( workingDir .. "/" .. v )
	end
end

This won’t include mounted content, like stuff from GMAs, though.

They can; for example here’s what it looks like when looking through individual addons:
https://dl.dropboxusercontent.com/u/26074909/tutoring/addon_info.txt

The way it’s done is by using file.Find( directory, title_of_mod_or_addon );

And yes, they are virtually merged with existing files.

Pardon me but do I use that like before or somehow else?



LoadDir("addons")


I’m very new to Lua and Gmod lua and well programming in general. Any help is greatly appreciated :3!

Edit:
I’m trying to add it in the /lua/autorun/server
part so… If that helps

I’ve tried to use file.Find like you describe, but I failed to get it working. Say I have an addon with some fonts that must be downloaded by clients. So the fonts are in folder “addons/<addonName>/resource/fonts”.

Using this, it doesn’t work:



local function getAddonFolderName()
	return string.match( debug.getinfo(1, "S").short_src, "/(.-)/" )
end
	
local function printFonts()
	local fonts = file.Find( "resource/fonts/*.ttf", getAddonFolderName() )
	for _, v in ipairs( fonts ) do
		print( "Found font " .. v )
	end
end
	
printFonts()


So I used this instead:



local function getAddonFolderName()
	return string.match( debug.getinfo(1, "S").short_src, "/(.-)/" )
end
	
local function printFonts()
	local fonts = file.Find( "addons/" .. getAddonFolderName() .. "/resource/fonts/*.ttf", "GAME" )
	for _, v in ipairs( fonts ) do
		print( "Found font " .. v )
	end
end
	
printFonts()


As an example if you use engine.GetAddons( ) then the resulting table you’d do addon.title - that’s what I mean by the title.

Well that is even more confusing…

So I’ve been messing around with FOX-WARRIOR’S Resources Generator but it doesn’t seem to work, I’ve tried making it /addons/<name>/path rather than <addon name>/path.

The server doesn’t seem to be downloading it and you all are confusing more more by the post…

So it’s just a normal thread now…