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?

Tandura
  • 866
  • 2
  • 7
  • 19

2 Answers2

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

Community
  • 1
  • 1
cshu
  • 5,654
  • 28
  • 44
  • 6
    @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. – Martin York May 19 '16 at 15:26
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();
}
Jose M. Jimenez
  • 101
  • 2
  • 7