BobS0327
The code you posted seems work, but as soon as all the files are searched it closes the program with exit status of 1. I'm trying to figure out why, or where it could be coming from.
BobS0327
The code you posted seems work, but as soon as all the files are searched it closes the program with exit status of 1. I'm trying to figure out why, or where it could be coming from.
The total variable was used to count the total number of files found.
And you're back to the same old code. You've split duplicate code into two branches. Bad.
I merged the two branches into one. Something akin to this. I haven't tested it, however.
And beware where I placed an assert to check for buffer overflows!Code:bool Search(const char* sFolder, int& fCount, int& dCount, int& total) { char* fBuffer = new char[256]; //storage for file name to be checked WIN32_FIND_DATA Files; //structure for handling all the files HANDLE hFiles = NULL; //handles for FindNextFile() strcpy(fBuffer, sFolder); //set up new folder to search in strcat(fBuffer, "\\*.*"); //find all files in new folder hFiles = FindFirstFile(fBuffer, &Files); //find the first, associated with files if (hFiles == INVALID_HANDLE_VALUE) { do { // Watch out for buffer overflows! assert( (strlen(sFolder) + 1 + strlen(Files.cFileName) + 1) <= 256 ); sprintf(fBuffer, "%s\\%s", sFolder, Files.cFileName); if (Files.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && (strcmp(Files.cFileName, ".") != 0) && (strcmp(Files.cFileName, "..") != 0)) { ++dCount; Search(fBuffer, fCount,dCount, total); } else { if((strcmp(Files.cFileName, ".") != 0) && (strcmp(Files.cFileName, "..") != 0)) { ++fCount; printf("cFileName : %s\n", Files.cFileName); } } } while(FindNextFile(hFiles,&Files) != 0); //{ // sprintf(fBuffer, "%s\\%s", sFolder, Files.cFileName); // if (Files.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && // (strcmp(Files.cFileName, ".") != 0) && // (strcmp(Files.cFileName, "..") != 0)) // { // ++dCount; // Search(fBuffer, fCount,dCount, total); // } // else // { // if((strcmp(Files.cFileName, ".") != 0) && (strcmp(Files.cFileName, "..") != 0)) // { // ++fCount; // printf("cFileName : %s\n", Files.cFileName); // } // } //} //FindClose(hFiles ); } delete [] fBuffer; return (hFiles != INVALID_HANDLE_VALUE); }
Unfortunately, your code doesn't even compile correctly, let alone process the folders recursively. I would strongly suggest that you read some tutorials on recursively processing files/folders. It's unfortunate that most of the time you don't know what you're talking about since you don't do your homework before posting a response to a thread.I merged the two branches into one. Something akin to this. I haven't tested it, however.
Finally, TEST your code postings before you post it to at least make sure it works.
This is untrue. The original example code was my own and it works 100% fine.
I DO know what I talk about most of the times.
Most people don't do that. Go blame everyone else.Finally, TEST your code postings before you post it to at least make sure it works.
If you don't want the code, then fine. It shows about how it should look. It's not a complete "make your homework for you".
I'll update the code to fix some compile errors.
Elysia
Your code works great, and does not have the problem of closing once it's done searching. It did however do that before I changed the assert compare size, and buffer size. I think it would close because the buffer got overloaded, could not hold the new path, then the new path was incomplete, and forced an error and closed. Anyways, I changed the values to 999 just to make sure there is plenty of room for a long path. I also noticed some things in there that would make it not work correctly, but i think that was done on purpose
I kind of feel bad cause I did not figure the end code out on my own, but yet I was obviously having a hard time. Thanks for all your help, especially Elysia!
My goodness. I just realized the code actually doesn't call CloseHandle >_<
That goodness for RAII, though. I'll fix that right now!
New code looks something like:
Code:hFileDirs = FindFirstFile(pArgs->strFolder + _T("\\*.*"), &fDataDirs); CHandle hhFileDirs(hFileDirs); if (hFileDirs == (HANDLE)-1) return false; // Apparently we couldn't search this directory... hFileFiles = FindFirstFile(pArgs->strFolder + _T("\\") + pArgs->strPattern, &fDataFiles); CHandle hhFileFiles(hFileFiles);
Are you talking about your original code you posted for me to look at?
Another question. How would I be able to tell once it is done searching? It's recursive so putting a call at the end of the function is out. I tried putting checks to see if it added a count to a file -and- directory, but once it hits the end of a folder it automatically does not increase either's count, so thats out. I thought maybe making the hFiles handle global and checking when it returns invalid but it never returns invalid as long as it is an existing directory. So.... what are the options here?
Last edited by scwizzo; 04-02-2008 at 03:09 PM.
Exactly, so everytime it reaches the end of a directory it will think it's done, when really it's not. It's only done if the function was not called anymore and all other functions are done, but that happens at the end of every directory. So I don't know how to make it know the searching is done.
But the point is, when it's done, it return true or false, until all the functions have finished.
It will recursively call itself for every directory, so when all those calls are done, the first instance of the function continues and it has found all the directories and files in its own directory and returns.
I don't see what the problem is?
Main won't continue until your function is finished.
I see what you're saying. Sometimes I think my brain just doesn't like to work.