Program doesn't copy files properly

时间:2015-07-31 20:34:03

标签: c++

It just copies the first letter of the file name and file type is .file instead of the real one.

The program can be used to sort and copy files into different directories (years and quarters) according to their 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.

1 个答案:

答案 0 :(得分:0)

Using only one global variable 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);
}
相关问题