Page 1 of 1
Searching directory
Posted: Fri Nov 07, 2008 12:24 am
by jesterguy
I've searched the great google for awhile now but not much luck, Anyways I'm trying to find a simple way to search through a directory and all subdirectories for files with a certain extension and load each one found.
Example of what I need: search through units folder, inside the folder are lots of other folders each one containing a lua file to create that unit.
Re: Searching directory
Posted: Fri Nov 07, 2008 8:24 am
by avansc
jesterguy wrote:I've searched the great google for awhile now but not much luck, Anyways I'm trying to find a simple way to search through a directory and all subdirectories for files with a certain extension and load each one found.
Example of what I need: search through units folder, inside the folder are lots of other folders each one containing a lua file to create that unit.
http://www.linuxquestions.org/questions ... ry-379323/
there are alot of ways of doing it. you could write a recursive methos that goes through, i can explain it if you want. but its a bit advanced.
or you can use the system("dir"); and build a structure yourself.
Re: Searching directory
Posted: Fri Nov 07, 2008 10:23 am
by MarauderIIC
It'd be platform-dependent, I think. I wrote a [linux] bash script for a class last semester (IIRC it generated an index for a webdir and made thumbnail pages for dirs containing images), I can post it when I get home if you think it'd help. But it has the basic components, just the directory listing command would be different. And the syntax.
If you're doing it under windows and your lua files are under c:\someproj\lua, perhaps something like (off the top of my head, not really checked for correctness)
Code: Select all
const string dirPrefix = "cd c:\someproj"
string files, dirs, line, file;
system(dirPrefix.c_str());
system("cd lua");
dirs = system("dir /B /AD"); //you need to capture the actual output though, which this wouldn't do // /B lists contents w/o timestamp info, /AD lists dirs only
getline(dirs, dir);
while (!dir.empty()) { //current dir we're trying to change to is not the end of the dir listing
system(dir); //change to a subdir of our current dir,
//probably needs something more like string("cd " + dir".c_str() or strcat(cd, dir); where cd is a char* = "cd "
files = system("dir *.lua /B");
getline(files, file); //something like that anyway. you want a single line of your output
while (!file.empty()) {
parse(file); //you want just the filename
process(file);
getline(files, file);
}
dirs = system("dir /B /AD"); //this will make this code go all the way down a branch
getline(dirs, dir);
dir = "cd " + dir; //change to subdir
//lack of a "cd .." here will make this code go all the way down one branch
//you need to make this code recurse so that you go all the way down every branch
}
http://www.computerhope.com/dirhlp.htm
Also,
http://www.codeguru.com/forum/showthread.php?t=357349