5

I want to copy the content o directory(tmp1) to another directory(tmp2). tmp1 may contain files and others directories. I want to copy the content of tmp1 (including the mode) using C/C++. If tmp1 contains a tree of directories I want to copy them recursively.

What is the simplest solution?

I found a solution to open the directory and read every entry and copy it with cp command. Any simpler solutions?

4
  • 1
    You might want to check this: stackoverflow.com/questions/2180079/… Commented May 19, 2016 at 14:23
  • @Mirakurun much more complex that what I suggested and does not copy the mode of the file (and not working for directories). Commented May 19, 2016 at 14:27
  • 1
    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. Commented May 19, 2016 at 14:45
  • I think the easiest way is to use the existing tools. Just use cp -R Commented May 19, 2016 at 15:24

2 Answers 2

9

I recommend using std::filesystem (merged to ISO C++ as of C++17!)

Shamelessly copied from http://en.cppreference.com/w/cpp/filesystem/copy:

std::filesystem::copy("/dir1", "/dir3", std::filesystem::copy_options::recursive);

Read more about it:

https://gcc.gnu.org/onlinedocs/gcc-6.1.0/libstdc++/api/a01832.html

experimental::filesystem linker error

Sign up to request clarification or add additional context in comments.

1 Comment

@Olaf: Don't penalize people because they want to help. Use the voting system to indicate the answer is good or not not (ie does it answer the question). You can penalize the OP for putting in no effort but that is not cshu's fault.
2

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(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.