Game Development Community

Looping through files in a folder

by Vern Jensen · in Torque Game Builder · 09/19/2007 (12:15 pm) · 2 replies

Let's say I have a path, such as:

/game/MyCustomStuff/paths/

and I want to loop through everything in that "paths" folder and get a path to each file in it, so I can open each file and load its contents into memory. How can this be accomplished?

I tried using some stuff in FileSystem, but this seems to be for *all* files in the entire game hierarchy. I want to limit my searches to a specific directory. I tried using setCurrentDirectory(), but it seems this function doesn't exist. Or I don't know how to call it properly. Ideas?

#1
09/19/2007 (12:41 pm)
Okay, I solved my problem. Turns out these functions use paths that go beyond what is just specific to the project. So I have to use "*" before my path name, such as:

%path = "/game/MyCustomStuff/paths/";
%filename = "*" @ %path @ "*.txt";

for (%file = findFirstFile(%filename); %file !$= ""; %file = findNextFile(%filename)) 
	echo( "The file found is" @ %file );

And that works like a charm.
#2
09/19/2007 (9:14 pm)
Glad you found your own solution :)