简体   繁体   English

程序不能正确复制文件

[英]Program doesn't copy files properly

It just copies the first letter of the file name and file type is .file instead of the real one.它只是复制文件名的第一个字母,文件类型是 .file 而不是真实的。

The program can be used to sort and copy files into different directories (years and quarters) according to their LastWriteTime.该程序可用于根据它们的 LastWriteTime 将文件排序和复制到不同的目录(年份和季度)。

void sort(std::string quartal, const char * c_pfad, SYSTEMTIME datum, WIN32_FIND_DATA *wfd_p){
            year = datum.wYear;
            str_year =  patch::to_string(year);             // convert

            year_path = dir + str_year + "\\";  // C:\\Pictures\\2015\\

            CreateDirectory(convert(year_path), NULL);                              // year-dir

            quartal_path = year_path + quartal; // C:\\Pictures\\2015\\Erstes Quartal\\

            CreateDirectory(convert(quartal_path), NULL);                               // quarter-dir

            ziel = quartal_path + *wfd_p->cFileName;

            CopyFile( c_pfad, convert(ziel), false ); 

}

void schleife(const char* file){
WIN32_FIND_DATA wfd;
HANDLE fHandle = FindFirstFile(file,&wfd);


do 
{ 
    // Neither . nor ..  (subdirectories) 
    if (!( (wfd.cFileName[0]=='.') && ( (wfd.cFileName[1]=='.' && wfd.cFileName[2]==0) || wfd.cFileName[1]==0 ) )) 
    { 
        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
        { 

              findDirGes = findDirGes.substr (0, findDirGes.size()-1) + wfd.cFileName + "\\*";
              schleife(convert(findDirGes));                             // no file but directory

        } 
        else 
        { 
           pfad = findDirGes.substr (0, findDirGes.size()-1) + wfd.cFileName;



          WIN32_FILE_ATTRIBUTE_DATA dateidaten; 

          GetFileAttributesEx(convert(pfad), GetFileExInfoStandard, &dateidaten); 
          // cprintf("\n %d", dateidaten.ftLastWriteTime); 

          FILETIME test = dateidaten.ftLastWriteTime; 
          // cprintf("\n %d", test); 
          SYSTEMTIME datum; 

          FileTimeToSystemTime(&test, &datum);


        switch (datum.wMonth){
        case 1:
        case 2:
        case 3:
            sort("Erstes Quartal\\", convert(pfad), datum, &wfd );
            break;
        case 4:
        case 5:
        case 6:
            sort("Zweites Quartal\\", convert(pfad), datum, &wfd );
            break;
        case 7:
        case 8:
        case 9:
            sort("Drittes Quartal\\", convert(pfad), datum, &wfd );
            break;
        case 10:
        case 11:
        case 12:
            sort("Viertes Quartal\\", convert(pfad), datum, &wfd );
            break;
  }

        } 
    } 
} 
while (FindNextFile(fHandle,&wfd)); 
FindClose(fHandle);
}

*The problem of the program is that it goes through all the files in the directory you typed in and if it comes to another directory it sorts and copies the files perfectly but after that it doesn't continue with the files (or directories) in the directory above. *该程序的问题在于它会遍历您输入的目录中的所有文件,如果涉及到另一个目录,它会完美地对文件进行排序和复制,但之后它不会继续处理其中的文件(或目录)上面的目录。

Thanks for your help.谢谢你的帮助。

Using only one global variable WIN32_FIND_DATA wfd;仅使用一个全局变量WIN32_FIND_DATA wfd; for all the recursive calls is an error.对于所有递归调用都是错误的。 Use a separate local variable each time you need to recurse.每次需要递归时使用单独的局部变量。

void schleife(const char* file)
{
    WIN32_FIND_DATA wfd; // local variable
    HANDLE fHandle = FindFirstFile(file, &wfd);
    do
    {
        ...
        schleife(convert(findDirGes)); // recursive call
        ...
    }
    while (FindNextFile(fHandle, &wfd)); 
    FindClose(fHandle);
}

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

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