Folder copy VC++

2019-06-25 05:21发布

问题:

i want to copy a directory from one drive to another drive. My selected directory contain many sub directories and files. How can i implement the same using vc++

回答1:

The SHFileOperation() API function is the workhorse function for copying files. It supports recursing directories. Review the options available in the SHFILEOPSTRUCT structure to control the copy.



回答2:

The hard way. copy every file individually.

Use FindFirst() and FindNext() to iterate over the content of a directory Use SetCurrentDirectory() to go in and out of directories
Use CreateDirectory() to create the new folders tree
and finally, use CopyFile() to copy the actual files



回答3:

If you have access to the boost library this is your friend:

http://www.boost.org/doc/libs/1_42_0/libs/filesystem/doc/index.htm

Check the tutorial for nice examples using a filesystem iterator.

To get you started:

#include <iostream>
#include “boost/filesystem.hpp”
int main(int argc, char *argv[])
  {
  boost::filesystem::path path1("/usr/local/include"); // your source path
  boost::filesystem::path::iterator pathI = path1.begin();
  while (pathI != path1.end())
    {
    std::cout << *pathI << std::endl; // here you could copy the file or create a directory
    ++pathI;
    }
  return 0;
  }