Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is wrong with the recursion

Status
Not open for further replies.

willi808

Programmer
Jun 11, 2003
14
DE
//search all subdirectories (recursion)
void SearchAllDir(char *argv1, char *argv2)
{

struct find_t sdir; //file attribute in a struct
char buffer[500]; //buffer of path (drive and directory)
char buffer2[500]; //buffer of path (drive and directory)
int result; //result of subdirectories

//arrays to NULL
*buffer = NULL;
*buffer2 = NULL;

//drive letter to buffer
strcpy(buffer,argv1);
//add ':\'
strcat(buffer,":\\");
//add path of directories
strcat(buffer,argv2);

// printf("%s\n",&buffer);
// printf("%s\n",&buffer2);

//buffer to buffer2 for dates
strcpy(buffer2, buffer);

//next level of subdirectories
strcat(buffer,"\\*.*");

//search first subdirectory in the next level
result = _dos_findfirst( buffer, _A_SUBDIR, &sdir);

//_dos_finfirst return '0' if true
while(result == 0)
{
//is there any subdirectory
if( sdir.attrib & _A_SUBDIR )
{
//no subdirectories which begins with '.' like '.' and '..'
if(sdir.name[0] != '.')
{
*buffer = NULL; //empty buffer
strcpy(buffer,argv2); //add complete path to buffer
strcat(buffer,"\\"); //add '\'
strcat(buffer,sdir.name); //add name of subdirectory

// printf("%s\n",&buffer);

//recursion (with new path)
SearchAllDir(argv1, buffer);

//read actual date und creation dates of subdirectories
if(SearchDates(argv1,buffer) == 1)
{
Wait();

//delete files in directory
DelFiles(argv1, buffer);

//delete directories
DelDir(argv1,buffer);

}

}

}

//if there is no lower level go to next directory
result = _dos_findnext(&sdir);
}

}
 
If you want us to debug your code for free, you could at least be more specific than "what is wrong..."



/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
sorry,

my problem is - it works goods until i come into the longest path. then it goes only one level down, go to the next subdirectory and at the last it goes one level down and to the end etc. it never steps up anymore.
and i really don't know what's my mistake...
 
Not sure you can use findnext etc recursively.

When I've recursed subdir, I've always done it like this (pseudo code):

void ProcessDir(path)
{
ListOfStrings dirs;
find_first(path+"*.*", fileInfo)
while(result)
{
if (fileInfo.isDir)
dirs.Add(fileInfo.name)
else
ProcessFile(fileInfo.name)
result = find_next(fileInfo)
}
find_close

for each dir in dirs
{
ProcessDir(path+dir.name)
}
}

/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top