简体   繁体   中英

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

I have a problem with my code. My findfile function doesn't show the actual name, but shows the folder name. Does anyone know what is causing the problem. path is going to be the downloads folder of the user, and path1 is the locations where the file needs to be moved. And yes I have files in that directory.

Code (for 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);
}

You have to append an asterik ( * ) (see MSDN FindFirstFile for more information) to your directory path ( szDir ) in order to enumerate all files and folders in the downloads folder. If you only want to enumerate the files then append *.* .

So change your code like this:

...

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

...

As @MRAB pointed out in the comments section you should also close the find handle with a call to FindClose(hFile) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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