Subj : Re: Sub-tree search To : borland.public.cpp.borlandcpp From : Georges Date : Sun Jul 03 2005 11:31 am Georges wrote: > Bob Gonder wrote: > > > 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. > > Bob, > Thanks for that. Tomorrow I'll write a little test program for it and > follow it through with the debugger to see what's going on at each stage. > Bit complex just to do by looking at it! I'll report back IDC. > Georges Bob, Two things - (1) It reports well on the named directory but i t doesn't scan sub-dirs in its present form! In the line: // then recurse through all the subfolders while (!done) { if( (fb.ff_attrib&FA_DIREC) ==FA_DIREC ) fb.ff_attrib is always a space (32). (2) We need the full address of found files to be returned. Hope you can help just this once more!.. Georges. ps - I've substituted 'FindClose' for 'findclose' as that was not known. .