Searching directory

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Searching directory

Post 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.
I will live forever or die trying.

"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Searching directory

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Searching directory

Post 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
Last edited by MarauderIIC on Fri Nov 07, 2008 11:16 am, edited 4 times in total.
Reason: see sig
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply