0

I have the following directory structure:

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

And I am looking to copy to a new directory. However, if the Parent folder already exists, regardless of it file contents, I do not want to copy it.

Main2
+-Parent2
  +-File7
  +-File8

So if I am copying from Main1 to Main2, the Parent2 folder in Main1 would not copy, nor would its contents.

In the end, it should end up looking like this:

Main1
+-Parent2
  +-File3
  +-File4

Main2
+-Parent1
  +-File1
  +-File2
+-Parent2
  +-File7
  +-File8
+-Parent3
  +-File5
  +-File6
...
rjbogz
  • 860
  • 1
  • 15
  • 36
  • Possible duplicate of [How can I get the list of files in a directory using C or C++?](http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c) – Jaroslav Kadlec Nov 28 '16 at 10:42

1 Answers1

2

This is the code I use to read the list of folders in any folder. You can use this to get what you requested.

// 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;
}
  • Ahh, awesome. I think this will work perfectly, thanks! I will just add in to copy if it doesn't exist. – rjbogz Apr 25 '16 at 14:04
  • Yeah, that's the gist of it ^^ Keep the functions separate though, so that each function does what it should :) Could you mark the answer as solved if this suffices? – Joey van Gangelen Apr 25 '16 at 14:05
  • Just wanted to test it out real quick, seems to work just fine though. Thanks again! – rjbogz Apr 25 '16 at 14:07