如何使用C ++(VS)将n-first文件移动到另一个目录

时间:2014-10-17 09:38:29

标签: c++ file directory console-application move

我的问题crossreferrer with [question] Visual C#: Move multiple files with the same extensions into another directory 但我还有最大的问题 - 我的目录包含超过300万个文件。 C#with .GetFiles()无法解决我的问题。 我认为,只有使用C ++我才能做到。 算法

  1. CreateDir" temp" + counterDirs
  2. FindFirst文件到源目录
  3. 将MoveFile移动到newDir(在步骤1中创建)
  4. 增加counterFiles
  5. 重复步骤2-4直到FindFirst没有错误(停止编程)或counterFiles< moveLimit(exmpl 100 000)
  6. 增加counterDirs
  7. 重复步骤1-6中的所有内容
  8. 目标:将所有文件从src-dir(300万)移动到一些目标dir,包含10万个文件。

    请帮我编码 - 我不懂C ++(VS 2010)

    这是我的代码

    `int _tmain(int argc, TCHAR *argv[])
    {
       WIN32_FIND_DATA ffd;
       LARGE_INTEGER filesize;
       TCHAR szDir[MAX_PATH];
       TCHAR szNewDir[MAX_PATH];
       TCHAR szNewDirEx[MAX_PATH];   
       size_t length_of_arg;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       DWORD dwError=0;
       DWORD dwFiles = 0;
       DWORD dwDirs = 0;
    
       // If the directory is not specified as a command-line argument,
       // print usage.
    
       if(argc != 2)
       {
          _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
          return (-1);
       }
    
       // Check that the input path plus 3 is not longer than MAX_PATH.
       // Three characters are for the "\*" plus NULL appended below.
    
       StringCchLength(argv[1], MAX_PATH, &length_of_arg);
    
       if (length_of_arg > (MAX_PATH - 3))
       {
          _tprintf(TEXT("\nDirectory path is too long.\n"));
          return (-1);
       }
    
       _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);
    
       // Prepare string for use with FindFile functions.  First, copy the
       // string to a buffer, then append '\*' to the directory name.
    
       StringCchCopy(szDir, MAX_PATH, argv[1]);
       StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
    
        StringCchCopy(szNewDir, MAX_PATH, argv[1]);
        StringCchCat(szNewDir, MAX_PATH, TEXT("\\dir"));
        StringCchCat(szNewDirEx, MAX_PATH, szNewDir);
       // Find the first file in the directory.
    
       hFind = FindFirstFile(szDir , &ffd);
    
       if (INVALID_HANDLE_VALUE == hFind) 
       {
          DisplayErrorBox(TEXT("FindFirstFile"));
          return dwError;
       } 
    
       // List all the files in the directory with some info about them.
    
       do
       {
          if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
          {
             _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
          }
          else
          {
                if(dwFiles == 0) {
                    StringCchCat(szNewDirEx, MAX_PATH, TEXT(dwDirs));
                    CreateDir(szNewDirEx);  //dwDirs toString
                }
    
                if (!MoveFileEx(__OLDDIR__ + ffd.cFileName, szNewDirEx + ffd.cFileName, MOVEFILE_WRITE_THROUGH))
                { 
                    printf ("MoveFileEx failed with error %d\n", GetLastError());
                    return;
                } else  {
                    dwFiles++;
                    if (dwFiles == 50000) {
                        dwDirs++;
                        dwFiles = 0;
                    }
    
                }
          }
       }
       while (FindFirstFile(hFind, &ffd) != 0);
    
       dwError = GetLastError();
       if (dwError != ERROR_NO_MORE_FILES) 
       {
          DisplayErrorBox(TEXT("FindFirstFile"));
       }
    
       FindClose(hFind);
       return dwError;
    }
    

1 个答案:

答案 0 :(得分:0)

我在How to use DirectoryInfo.GetFiles and have it stop after finding the first match?找到了解决我的任务的方法。我将尝试在我的目录中运行它,稍后将发布结果。

完整。线程链接中的代码完美无缺。