views:

209

answers:

4
+1  Q: 

Folder copy VC++

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++

A: 

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.

Computer Guru
-1, see nobugz reply
peterchen
+1  A: 

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

shoosh
A: 

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;
  }
nabulke
+2  A: 

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.

Hans Passant
SHFileOperation is officially deprecated on Windows Vista+ and its delete feature unofficially "just doesn't work" on Vista+.
Computer Guru
Thanks for ur valuable help..Its working fine
It is *not* deprecated. Vista merely has another mousetrap to do this.
Hans Passant
@sjith: great. Consider this an opportunity to fix the horrid 0% accept rate that's on your name.
Hans Passant