views:

27

answers:

1

The documentation for CFindFile states that

Nonzero if there are more files; zero if the file found is the last one in the directory or if an error occurred. To get extended error information, call the Win32 function GetLastError. If the file found is the last file in the directory, or if no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

So how do I know if I have 1 file or 0 files if a call to FindNextFile returns the same value?

It seems that a call to FindFile::GetFilePath() fails (which inadvertently causes my application to crash) if zero files are found.

pLog->Log(_T("Finding files in [%s]"), 1, szFilePath);

    if (!oFindFile.FindFile(szFilePath, 0))
{
    pLog->Log(_T("Failed to find file in directory: [%s]"),1,szDirectory);
    return false;
}

bool moreFiles = true;
while(moreFiles)
{
    moreFiles = oFindFile.FindNextFile();
    if (oFindFile.IsDots())
    {
        continue;
    }

    CString szFileName = oFindFile.GetFilePath();
    pLog->Log(_T("Found file [%s]"), 1, szFileName);
    pVector->push_back(szFileName);
}
return true;

Edit

CString szFilePath = _T("C:\documents and settings\username\desktop\*.lnk");
CString szDirectory = T("C:\documents and settings\username\desktop");
+3  A: 

If there are no files, your call to CFileFind::FindFile will return false. You need to call this before you can call FindNextFile.

dwo
Hmmm alright. I am calling that function. Maybe I'm doing something else wrong...
Adam Driscoll
Can you post some code?
dwo
Sure. The first log statement is called but the second (within the if block) is not called.
Adam Driscoll
What is the content of szFilePath and szDirectory?
dwo
Added to the question.
Adam Driscoll
Wait....hold your horses. I think I know what it is and it has to do with szDirectory...
Adam Driscoll
How many files/matches are in the directory?
dwo
Missing the double backslash \\ ?
dwo
There are 0 matches in the directory. It happens that szDirectory actually had unexpanded envrionment variables in it; which for some reason was killing the logging mechanism. That is why there is no log :). Thanks for the trouble shooting ideas though it really helped me track this down. Now to look at this stupid logging stuff..
Adam Driscoll