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++
No Windows API provides copying of directories + contents.
You'll need to write a function that recursively copies the contents of each folder. When given a folder, scan through its contents and call the same function for each item.
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
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;
}
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.