Subj : Re: Sub-tree search To : borland.public.cpp.borlandcpp From : Bob Gonder Date : Sat Jul 02 2005 01:12 pm Georges wrote: >I've been trying to write something that will search a directory-tree >for named files such as *.cpp - alas without much success! > >A sort of OWL/C++ version of DOS 'dir *.cpp /s' > >If anyone has any ideas, or better still some code! It would be very >much appreciated. Start with where you want to start: int main(void) { FindCPP( "C:\\"); return 0; } Then, find the files: void FindCPP( char*path) { static depth = 0;//for illustration use if you want int done; char mypath[256]; struct ffblk fb; fnmerge( mypath,NULL,path,"*.cpp",NULL); done = findfirst( mypath, &fb, 0 ); // first, find all the files in this folder while (!done) { // you could use depth here to indent the display printf(" %s\n", fb.ff_name); done = findnext(&fb); } findclose( &fb); fnmerge( mypath,NULL,path,"*.*",NULL); done = findfirst( mypath, &fb, 0 ); // then recurse through all the subfolders while (!done) { if( (fb.ff_attrib&FA_DIREC) ==FA_DIREC ) { if( fb.ff_name[0] != '.' ) // ignore '.' and '..' dirs { fnmerge( mypath, NULL,path,fb.ff_name,"\\"); ++depth; FindCPP( mypath ); --depth; } } done = findnext(&fb); } findclose( &fb); } Note that this is just written, and untested. There may be typeos, and the fnmerge functions might need added slashes between the old path and the new ff_name, in which case, use strcpy/strcat instead. A good defense woulde be to check the passed path for an ending '\\' at the very beginning of the function. .