简体   繁体   English

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

[英]Displaying all the files in a folder using win32 C++

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.我有一个包含大约 9 个文件的文件夹,当按下按钮时,我试图让我的代码显示文件夹中每个文件的消息框以及文件名。 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).这是来自我创建的错误处理程序 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 ;您的while语句有错误; 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.因此,您循环而不处理FindFileData ,直到FindNextFile()失败,然后您处理报告的最后一个FindFileData数据。 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.该显示最终会清除失败的FindNextFile()报告的错误代码,这就是为什么当您实际检索错误代码时不再是ERROR_NO_MORE_FILES的原因。

You need to remove that erroneous ;您需要删除该错误; . .

Also, you should use a do..while loop instead.此外,您应该改用do..while循环。 Using a while loop will skip the 1st file that FindFirstFile() reports.使用while循环将跳过FindFirstFile()报告的第一个文件。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM