Recently I had the same need, so I have developed the next chunk of code in order to solve the problem. I hope it helps to another people in the same situation.
#include <iostream> #include <dirent.h> #include <string.h> #include <sys/stat.h> #include <windows.h> using namespace std; bool is_dir(const char* path); void copyFile_(string inDir, string outDir); void copyDir_(const char *inputDir, string outDir); int main() { string srcDir = "C:\\testDirectory"; string destDir = "C:\\destDir"; copyDir_(srcDir.c_str(), destDir); return 0; } void copyDir_(const char *inputDir, string outDir) { DIR *pDIR; struct dirent *entry; string tmpStr, tmpStrPath, outStrPath, inputDir_str = inputDir; if (is_dir(inputDir) == false) { cout << "This is not a folder " << endl; return; } if( pDIR = opendir(inputDir_str.c_str()) ) { while(entry = readdir(pDIR)) // get folders and files names { tmpStr = entry->d_name; if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ) { tmpStrPath = inputDir_str; tmpStrPath.append( "\\" ); tmpStrPath.append( tmpStr ); cout << entry->d_name; if (is_dir(tmpStrPath.c_str())) { cout << "--> It's a folder" << "\n"; // Create Folder on the destination path outStrPath = outDir; outStrPath.append( "\\" ); outStrPath.append( tmpStr ); mkdir(outStrPath.c_str()); copyDir_(tmpStrPath.c_str(), outStrPath); } else { cout << "--> It's a file" << "\n"; // copy file on the destination path outStrPath = outDir; outStrPath.append( "\\" ); outStrPath.append( tmpStr ); copyFile_(tmpStrPath.c_str(), outStrPath.c_str()); } } } closedir(pDIR); } } bool is_dir(const char* path) { struct stat buf; stat(path, &buf); return S_ISDIR(buf.st_mode); } void copyFile_(string inDir, string outDir) { CopyFile(inDir.c_str(), outDir.c_str(), 1); DWORD Error = GetLastError(); }
cp -a $src $dst- Otherwise: what have you done already? Show your code. This is no consulting site. Also C and C++ are different languages. Pick one.cp -R