I have a folder with about 9 files in it and I am trying to get my code to display Messageboxes for each file in the folder with the name of the file, when a button is pressed. For some reason, it only displays a messagebox for the very last file in the folder and then gives me the error: "Error finding files." which is from the error handler I created if (fileError != ERROR_NO_MORE_FILES). I am not sure why it is not displaying all the file names and giving me an error. The relevant part of my code is below:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
TCHAR file_buff[200];
TCHAR file_buff2[200];
TCHAR file_buff3[200];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
DWORD fileError_initial = 0;
DWORD fileError = 0;
In the button press that should give me all the messageboxes:
case IDC_BUTTON_RUN:
hFind = FindFirstFile(L"C:\\Users\\sallen\\Desktop\\Folder1\\*", &FindFileData);
fileError_initial = GetLastError();
if (hFind == INVALID_HANDLE_VALUE)
{
wsprintfW(file_buff, L"No files were found.\n Error: %s.", fileError_initial);
MessageBox(NULL, file_buff, L"File Search Error", 0);
return fileError_initial;
}
else
{
while (FindNextFile(hFind, &FindFileData) != 0);
{
wsprintf(file_buff2, L"The first file found is %s.\n", FindFileData.cFileName);
MessageBox(hWnd, file_buff2, L"File Name", 0);
}
fileError = GetLastError();
if (fileError != ERROR_NO_MORE_FILES)
{
wsprintfW(file_buff3, L"Error finding files. %s", fileError);
MessageBox(hWnd, file_buff3, L"File Search Error", 0);
}
}
FindClose(hFind);
return fileError;
break;
Your while
statement has an erroneous ;
on it:
while (FindNextFile(hFind, &FindFileData) != 0); // <-- here
Thus, you loop without processing FindFileData
until FindNextFile()
fails, AND THEN you process the last FindFileData
data that was reported. And that display ends up wiping out the error code that the failed FindNextFile()
reported, which is why the error code is no longer ERROR_NO_MORE_FILES
when you actually retrieve it.
You need to remove that erroneous ;
.
Also, you should use a do..while
loop instead. Using a while
loop will skip the 1st file that FindFirstFile()
reports.
Try this instead:
case IDC_BUTTON_RUN:
hFind = FindFirstFile(L"C:\\Users\\sallen\\Desktop\\Folder1\\*", &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
fileError = GetLastError();
if (fileError == ERROR_FILE_NOT_FOUND)
{
fileError = 0;
MessageBox(NULL, L"No files were found.", L"File Search Error", MB_OK | MB_ICONWARNING);
}
else
{
wsprintfW(file_buff, L"Error finding files. %d", fileError);
MessageBox(hWnd, file_buff, L"File Search Error", MB_OK | MB_ICONERROR);
}
return fileError;
}
do
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
MessageBox(hWnd, FindFileData.cFileName, L"File Name", MB_OK | MB_ICONINFORMATION);
}
}
while (FindNextFile(hFind, &FindFileData));
fileError = GetLastError();
FindClose(hFind);
if (fileError != ERROR_NO_MORE_FILES)
{
wsprintfW(file_buff, L"Error finding files: %d", fileError);
MessageBox(hWnd, file_buff, L"File Search Error", MB_OK | MB_ICONERROR);
}
else
fileError = 0;
return fileError;
UPDATE: to handle sub-folders, you will have to move your code into a recursive function, eg:
DWORD SearchFolder(LPCTSTR folder)
{
WIN32_FIND_DATA FindFileData;
TCHAR mask[MAX_PATH+3], filePath[MAX_PATH];
DWORD dwError, dwResult;
PathCombine(mask, folder, L"*");
HANDLE hFind = FindFirstFile(mask, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return GetLastError();
dwResult = ERROR_NO_FILES_FOUND;
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (lstrcmp(FindFileData.cFileName, ".") == 0 || lstrcmp(FindFileData.cFileName, "..") == 0)
continue;
PathCombine(filePath, folder, FindFileData.cFileName);
dwError = SearchFolder(filePath);
if (dwError != NO_ERROR && dwError != ERROR_NO_FILES_FOUND) {
SetLastError(dwError);
break;
}
}
else
{
// do something with FindFileData.cFileName ...
PathCombine(filePath, folder, FindFileData);
...
dwResult = NO_ERROR;
}
}
while (FindNextFile(hFind, &FindFileData));
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
return dwError;
return dwResult;
}
...
case IDC_BUTTON_RUN:
{
DWORD dwResult = SearchFolder(L"C:\\Users\\sallen\\Desktop\\Folder1\\");
if (dwResult != NO_ERROR)
{
if (dwResult == ERROR_NO_FILES_FOUND) {
MessageBox(NULL, L"No files were found.", L"File Search Error", MB_OK | MB_ICONWARNING);
}
else {
swprintf(file_buff, L"Error finding files. %u", dwError);
MessageBox(hWnd, file_buff, L"File Search Error", MB_OK | MB_ICONERROR);
}
}
return dwError;
}