Windows - 仅在父目录不存在时才复制文件

时间:2016-04-25 13:41:06

标签: windows copy paste exists

我有以下目录结构:

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

我希望复制到一个新目录。但是,如果父文件夹已存在,无论文件内容如何,​​我都不想复制它。

Main2
+-Parent2
  +-File7
  +-File8

因此,如果我从Main1复制到Main2Parent2中的Main1文件夹将不会复制,也不会复制其内容。

最后,它应该看起来像这样:

Main1
+-Parent2
  +-File3
  +-File4

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

1 个答案:

答案 0 :(得分: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;
}
相关问题