Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read files one by one in a folder. This is what i have:

WIN32_FIND_DATA FindFileData; 
	HANDLE hFind = INVALID_HANDLE_VALUE; 
	LPCWSTR lpath = _T("C:\\Program Files\\CSFT\\Manual");
        CString FoundFileName;

	hFind = FindFirstFile(lpath, &FindFileData);
	int found = -1;

	if (hFind == INVALID_HANDLE_VALUE) 
	{
		return;
	} 
	else 
	{
	        while (hFind && found < 0)
		{
			FoundFileName = FindFileData.cFileName;
			found = FoundFileName.Find(L"Iv-User Manual", 0);
			FindNextFile(hFind, &FindFileData);
		}
	}


When i am debugging, my hFind is not an invalid handle, but FoundFileName is always "Manual"(FindFileData.cFileName) and the loop never ends, i have only 4 PDF files in the folder.

Help pls.
Posted

After FindFirstFile you should call FindNextFile, to get first file name.

BOOL local_result = FindNextFile(hFind, &FindFileData);

Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.
 
Share this answer
 
v2
Comments
Andrew Brock 11-Feb-11 9:54am    
Strange as this may be, it is correct and should fix the problem. 5+.
amarasat 11-Feb-11 9:57am    
No Luck, even calling the FindNextFile to get the first file name, FindFileData.cFileName is "Manual" and my Local_result is 0.

The first problem is i have 4 PDF files with different names, but its returning Manual as the first file name. This is what i have. dwErr1 is 18.

<pre>hFind = FindFirstFile(lpath, &FindFileData);
BOOL Local_result = FindNextFile(hFind, &FindFileData);

DWORD dwErr1 = GetLastError();

while (hFind && Local_result) --> Local_result is zero here.
{
FoundFileName = FindFileData.cFileName;
found = FoundFileName.Find(L"Iv5-User Manual", 0);
Local_result = FindNextFile(hFind, &FindFileData);
}</pre>
amarasat 11-Feb-11 10:02am    
This works for C++ also right?
MSDN[^] says:

ERROR_NO_MORE_FILES
18 (0x12)
There are no more files.

Looking at your code again, you need to provide a wildcard path.

WIN32_FIND_DATA FindFileData; 
HANDLE hFind = INVALID_HANDLE_VALUE; 
LPCWSTR lpath = _T("C:\\Program Files\\CSFT\\Manual*");
//or *.pdf if you only want pdf files (this will exclude directories that don't end in .pdf too)
CString FoundFileName;
hFind = FindFirstFile(lpath, &FindFileData);
int found = -1;
if (hFind == INVALID_HANDLE_VALUE) {
	return;
} else {
	while (hFind && found < 0)
	{
		FoundFileName = FindFileData.cFileName;
		found = FoundFileName.Find(L"Iv-User Manual", 0);
		FindNextFile(hFind, &FindFileData);
	}
}


Additionally, what Kozlov said still holds, you need
 
Share this answer
 
v2
Comments
[no name] 24-Mar-11 5:56am    
Corrected my surname.
Andrew Brock 24-Mar-11 6:10am    
Sorry. That's my bad.
You also need to check the return value for FindNextFile (you don't seem to be doing that).
 
Share this answer
 
Comments
amarasat 11-Feb-11 9:58am    
You are right, i am checking the Boolean value now!!
Sergey Alexandrovich Kryukov 11-Feb-11 13:54pm    
I up-voted by my 5, because a vote of "3" is idiotic: you point out important thing.
--SA
Nish Nishant 11-Feb-11 17:57pm    
Thanks Kryukov.
MSDN[^] says:

ERROR_NO_MORE_FILES
18 (0x12)
There are no more files.

Looking at your code again, you need to provide a wildcard path.

WIN32_FIND_DATA FindFileData; 
HANDLE hFind = INVALID_HANDLE_VALUE; 
LPCWSTR lpath = _T("C:\\Program Files\\CSFT\\Manual\\*");
//or *.pdf if you only want pdf files (this will exclude directories that don't end in .pdf too)
CString FoundFileName;
hFind = FindFirstFile(lpath, &FindFileData);
int found = -1;
if (hFind == INVALID_HANDLE_VALUE) {
	return;
} else {
	while (hFind && found < 0)
	{
		FoundFileName = FindFileData.cFileName;
		found = FoundFileName.Find(L"Iv-User Manual", 0);
		FindNextFile(hFind, &FindFileData);
	}
}


Additionally, what Kizlov said still holds, you need to call FindNextFile first.
 
Share this answer
 
v2
I got the solution, thanks everyone for helping, the problem is with what we are searching for:

LPCWSTR lpath = _T("C:\\Program Files\\Manual\\");

This doesn't do anything

LPCWSTR lpath = _T("C:\\Program Files\\Manual\\*");

This means search for a directory by name *

LPCWSTR lpath = _T("C:\\Program Files\\Manual\\*.");

This means search for all files, but also returning directory names not sure abt it

LPCWSTR lpath = _T("C:\\Program Files\\Manual\\*.pdf");

This means serching especially for pdf files.

but this was never specified in microsoft docuemnts, hate them.

FindNextFile returns every next file and return value helps in continuing the loop or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900