2016-04-25 35 views
0

我有以下目錄結構:窗口 - 將文件複製僅當父目錄不存在

Main1 
+-Parent1 
    +-File1 
    +-File2 
+-Parent2 
    +-File3 
    +-File4 
+-Parent3 
    +-File5 
    +-File6 
... 

而且我期待複製到一個新的目錄。但是,如果父文件夾已存在,無論文件內容如何,​​我都不想複製它。

Main2 
+-Parent2 
    +-File7 
    +-File8 

所以,如果我從Main1複製到Main2,在Main1Parent2文件夾不會複製,也不會其內容。

最後,它應該結束這樣看:

Main1 
+-Parent2 
    +-File3 
    +-File4 

Main2 
+-Parent1 
    +-File1 
    +-File2 
+-Parent2 
    +-File7 
    +-File8 
+-Parent3 
    +-File5 
    +-File6 
... 
+0

的可能的複製([我如何使用C或C++?獲得文件的目錄列表] http://stackoverflow.com/questions/612097/how-can-i-get-the-列表中的文件在目錄中使用c或c) –

回答

2

這是我用來讀取任意文件夾中的文件夾列表中的代碼。你可以用它來得到你所要求的。

// http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c herohuyongtao 
std::vector<std::string> get_all_folder_names_within_folder(std::string folder) 
{ 
    std::vector<std::string> names; 
    char search_path[200]; 
    sprintf_s(search_path, 200, "%s/*.*", folder.c_str()); 
    WIN32_FIND_DATA fd; 
    HANDLE hFind = ::FindFirstFile(search_path, &fd); 
    int i = 0; 
    if(hFind != INVALID_HANDLE_VALUE) { 
     do { 
      // read all (real) files in current folder 
      // , delete '!' read other 2 default folder . and .. 
      if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 
       if (i >= 2){ 
        names.push_back(fd.cFileName); 
       } 
      } 
      i++; 
     }while(::FindNextFile(hFind, &fd)); 
     ::FindClose(hFind); 
    } 
    return names; 
} 
+0

啊,真棒。我認爲這將工作完美,謝謝!如果它不存在,我將添加到複製中。 – rjbogz

+0

是的,這是它的要點^^保持功能分開,但每個功能都應該做它應該:)如果這足夠了,你能標記答案解決嗎? –

+0

只是想快速測試它,但似乎工作得很好。再次感謝! – rjbogz

相關問題