Click to See Complete Forum and Search --> : MFC : CFileFind


June 12th, 1999, 09:06 AM
Hi, I am using this class 'CFileFind' to read in all files of a certain extension. Below is the code :

CFileFind oFinder;
CString oFilename;

/* Search for files */
if (oFinder.FindFile ("*.SAV") != FALSE)
{
/* Loop until no file is found */
while (oFinder.FindNextFile () == TRUE)
{
/* Get filename */
oFilename = oFinder.GetFileName ();

// Here i will use the filename to load the data
.
.
}

/* End the earch */
oFinder.Close ();
}

The problem with this is that i will always read in 1 file less, e.g 20 files found, only 19 read in. I suspected I need to get the file name immediately after the 'FileFind' function is successful but in the Microsoft Help documentation, the 'GetFileName' function can be only called when 'GetNextFile' function is called at least once.

Hope that you guys can help me. Thanks!!

Oliver Kinne
June 12th, 1999, 09:32 AM
The problem with CFileFind.GetNextFile() is that it returns FALSE with the last file it finds. Thus, even though there is another file, your code exits. Do the following instead:


BOOL bFilesLeft = TRUE;

/* Loop until no file is found */
do
{
bFilesLeft = oFinder.FindNextFile ();

/* Get filename */
oFilename = oFinder.GetFileName ();

// Here i will use the filename to load the data
.
.
} while (bFilesLeft);





Hope this helps?
Oliver.