使用win32 C++显示文件夹中的所有文件

Displaying all the files in a folder using win32 C++

我有一个包含大约 9 个文件的文件夹,我正在尝试让我的代码在按下按钮时显示文件夹中每个文件的消息框以及文件名。出于某种原因,它只显示文件夹中最后一个文件的消息框,然后给出错误:“查找文件时出错。”它来自我创建的错误处理程序 if (fileError != ERROR_NO_MORE_FILES)。我不确定为什么它不显示所有文件名并给我一个错误。我的代码的相关部分如下:

#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;

在应该给我所有消息框的按钮中:

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;

您的 while 语句有一个错误的 ;

while (FindNextFile(hFind, &FindFileData) != 0); // <-- here

因此,您循环不处理 FindFileData 直到 FindNextFile() 失败,然后您处理最后报告的 FindFileData 数据。该显示最终消除了失败 FindNextFile() 报告的错误代码,这就是为什么当您实际检索时错误代码不再是 ERROR_NO_MORE_FILES

你需要删除那个错误的;

此外,您应该改用 do..while 循环。使用 while 循环将跳过 FindFirstFile() 报告的第一个文件。

试试这个:

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:要处理子文件夹,您必须将代码移动到递归函数中,例如:

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;
}