This is basically a continuation of my old thread about file searching, but since I was completely and utterly wrong on the thought behind it I made a new thread. Old thread is here.
I looked over Elysia's example of looking through the files and every time I get a better understanding of it. I basically copied the example in my own terms in to my program, and now it comes up with a windows error (the "Send Error Report", "Don't Send" dialog box). I've tried eliminating things one at a time to see what the problem is but nothing points in a new direction. Where is my code coming up with this error?
Here is Elysia's...
Code:
bool FindFilesInternal(pp<FindFilesInternalStruct> pArgs, pp< vector<CString> > s_vFiles, UINT64& rCurrentCount, UINT64& rTotalCount)
{
WIN32_FIND_DATA fDataDirs, fDataFiles;
HANDLE hFileDirs = NULL, hFileFiles = NULL;
if (pArgs->strFolder.Right(1) == _T("\\")) pArgs->strFolder.Delete(pArgs->strFolder.GetLength());
hFileDirs = FindFirstFile(pArgs->strFolder + _T("\\*.*"), &fDataDirs);
if (hFileDirs == (HANDLE)-1) return false; // Apparently we couldn't search this directory...
hFileFiles = FindFirstFile(pArgs->strFolder + _T("\\") + pArgs->strPattern, &fDataFiles);
if (hFileFiles != (HANDLE)-1)
{
while (FindNextFile(hFileFiles, &fDataFiles) && GetLastError() == ERROR_NO_MORE_FILES) // Search for files in local directory
{
if (pArgs->pCancel && *pArgs->pCancel) return true;
if ( !(fDataFiles.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
rCurrentCount++;
rTotalCount++;
if (*pArgs->p_bRequest) // Due to thread safety, data has to be requested. Local copies of everything is kept. This avoids expensive locking.
{
if (pArgs->p_nCurrentFile) *pArgs->p_nCurrentFile = rCurrentCount;
if (pArgs->p_strCurrentFile) *pArgs->p_strCurrentFile = pArgs->strFolder + _T("\\") + fDataFiles.cFileName;
if (pArgs->pTotalFiles) *pArgs->pTotalFiles = rTotalCount;
*pArgs->p_bRequest = false;
SetEvent(pArgs->hRequestDataEvent);
}
if (!pArgs->bCount)
s_vFiles->push_back(pArgs->strFolder + _T("\\") + (CString)fDataFiles.cFileName);
}
}
}
if (pArgs->bSearchSubFolders)
{
while (FindNextFile(hFileDirs, &fDataDirs) && GetLastError() == ERROR_NO_MORE_FILES) // Search for directories
{
if (pArgs->pCancel && *pArgs->pCancel) return true;
if ((fDataDirs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && _tcscmp(fDataDirs.cFileName, _T(".")) != 0 && _tcscmp(fDataDirs.cFileName, _T("..")) != 0)
{
ppnew<FindFilesInternalStruct> pArgs2;
*pArgs2 = *pArgs;
pArgs2->strFolder = pArgs->strFolder + _T("\\") + fDataDirs.cFileName;
FindFilesInternal(pArgs2, s_vFiles, rCurrentCount, rTotalCount);
}
}
}
return true;
}
Here is mine... It might look choppy cause I am still learning.
Code:
bool Search(SEARCHFILES pArgs, int &fCount, int &dCount, int &total){
LPSTR lBuffer; //storage for _getdcwd
LPSTR buffer; //displayed storage for _getdcwd
LPSTR pbuffer; //storage for parent
LPSTR fBuffer; //storage for file name to be checked
WIN32_FIND_DATA Files,Dirs; //structure for handling all the files
HANDLE hFiles,hDirs; //handles for FindNextFile()
strcat(fBuffer,pArgs.StartFolder); //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
hDirs = FindFirstFile(fBuffer,&Dirs); //find the first, associated with directories
if(hFiles == INVALID_HANDLE_VALUE) return false; //if the file handle is invalid, return false
else if(hFiles != INVALID_HANDLE_VALUE)
{
while(FindNextFile(hFiles,&Files) && GetLastError() != ERROR_NO_MORE_FILES)
{
if(Files.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
fCount++; //increase file count
//SendMessage(hStatus,SB_SETTEXT,1,(LPARAM)"files searched: %d fCount"); //this might be wrong
//but it's not the cause of the problem (editted out)
fBuffer = _getcwd(lBuffer,MAX_PATH); //get current directory
strcat(fBuffer,Files.cFileName); //get full path for file (NOT USED YET)
SetDlgItemText(Hwnd,TEST,Files.cFileName); //show the current file
}
}
}
else{
SetDlgItemText(Hwnd,TEST,"!= INVALID_HANDLE_VALUE");
return false;
}
if(hDirs == INVALID_HANDLE_VALUE) return false;
else if(hDirs != INVALID_HANDLE_VALUE)
{
while(FindNextFile(hDirs,&Dirs) && GetLastError() != ERROR_NO_MORE_FILES)
{
if((Dirs.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)){
if(!(strcmp(Dirs.cFileName,"..") == 0 || (strcmp(Dirs.cFileName,".") == 0)))
{
SEARCHFILES pArgs2; //create new structure
pArgs2 = pArgs; //assign new structure the old structure
pbuffer = _getcwd(lBuffer,MAX_PATH); //get the current directory
strcat(pbuffer,Dirs.cFileName); //add the new directory to it
pArgs2.StartFolder = pbuffer; //assign new directory that is to be search
SetDlgItemText(Hwnd,CUR_PATH,pbuffer); //show the new directory
Search(pArgs2,fCount,dCount,Total); //recursively call the function again with a new directory
}
}
}
}
else{
SetDlgItemText(Hwnd,TEST,"!= INVALID_HANDLE_VALUE");
return false;
}
return true;
}