查找所有文件时不显示文件名

File name doesn't show up when looking for all files

本文关键字:显示 显示文件 文件名 文件 查找      更新时间:2023-10-16

我的代码有问题。我的查找文件函数不显示实际名称,但显示文件夹名称。有谁知道是什么导致了问题。path 将是用户的下载文件夹,path1 是需要移动文件的位置。是的,我在那个目录中有文件。

代码(适用于窗口):

bool* pointer = &doen;
WORD wait = 2500;
string path1 = getCurrentPath();
char userName[10];
DWORD userNameSize = sizeof(userName);
GetUserName(userName, &userNameSize);
string path = path1.substr(0, 3);
path += "users\";
path += userName;
path1 = path;
path += "\downloads";
path1 += "\documents\xxxx";
char const* plaatsD = path.c_str();
char const* plaatsF = path1.c_str();
userNameSize = NULL;
WIN32_FIND_DATA ffd;
TCHAR szDir[MAX_PATH];
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
string str;
string str2;
string str3;
char const* a;
char const* b;
StringCchCopy(szDir, MAX_PATH, plaatsD);
while (herhalen)
{
    Sleep(wait);
    hFind = FindFirstFile(szDir, &ffd);
    if (INVALID_HANDLE_VALUE == hFind) 
        continue;
    do
    {
        str = ffd.cFileName;
        if (str.find("xxx") != string::npos)
        {
            str2 = path;
            str2 += "\" + str + ".b";
            str3 = path1;
            str3 += "\" + str + ".b";
            a = str2.c_str();
            b = str3.c_str();
            try
            {
                CopyFile(a, b, true);
            }
            catch (exception)
            { 
            }
            a = NULL;
            b = NULL;
        }
    } 
    while (FindNextFile(hFind, &ffd) != 0);
}
您必须将

星号 ( * ) (有关详细信息,请参阅 MSDN FindFirstFile) 到目录路径 ( szDir ) 才能枚举下载文件夹中的所有文件和文件夹。如果只想枚举文件,则追加*.*

因此,请像这样更改代码:

...
path += "users\";
path += userName;
path += "\*";      // Append an asterik.
...

正如@MRAB评论部分指出的那样,您还应该通过调用 FindClose(hFile) 来关闭查找句柄。