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

时间:2012-07-04 18:32:43

标签: c++ file directory find

我的代码有问题。我的findfile函数不显示实际名称,但显示文件夹名称。有谁知道导致问题的原因。 path将成为用户的downloads文件夹,path1是需要移动文件的位置。是的,我在该目录中有文件。

代码(用于Windows):

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

1 个答案:

答案 0 :(得分:2)

您必须将asterik(*)(有关详细信息,请参阅MSDN FindFirstFile)附加到目录路径(szDir),以便枚举下载中的所有文件和文件夹夹。如果您只想枚举文件,请附加*.*

所以改变你的代码:

...

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

...

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