• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

How to copy entire folder structure from C++?

Is there a Windows API function that will copy an entire folder structure?  I am particularly interested in a solution that doesn't abort if one of the files can't be copied because of a sharing vilation.

Only an answer that will give me the above solution will be accepted.  If there's no way to do this via a Windows API call, then a code snippet will be accepted, otherwise the answer will be rejected.
1 Solution

There is no Windows API. What you can do is for a folder repetedly call FindFirstFile/FindNextFile and copy the files (with CopyFile), and when a directory is found, call the same function recursively.

I cannot give you a code snippet right now because I don't remember the parameters to all those APIs, but if you are interested, I can look them up in help and write those functions for you. I work in C/C++, so if you work in another language, I could write a simple DLL for you.

Anyway, send me an e-mail to luisr@iname.com if you are interested.

How about using the DOS command xcopy with the /s option?
You can fire it off from your c++ code using the CreateProcess API.
Hmm - this can be done much easier, 'SHFileOperation()' should do the job...
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

shrifAuthor Commented:
I'm not aware of a single API that will solve your problem but here is a
propose solution

int CopyFiles(char *szFromDir, char *szToDir)
      long hFile;
      struct _finddata_t fd;
      char szBuffer[256];

      sprintf(szBuffer,"%s\\*.*", szFromDir);
      if( (hFile = _findfirst(szBuffer, &fd)) == -1L ) return(-1);

            char szFrom[256],szTo[256];
            if(*fd.name !='.')

                  //check for subdirectories

                  //check for volume space remaining
                  CFileStatus status;
                  CFile::GetStatus(szFileName, status );
                  if(status.m_attribute==      0x10) //directory

    }while(_findnext(hFile, &fd) == 0);
    _findclose( hFile );


shrifAuthor Commented:
jkr, repost as an 'answer'

Thanx shrif ;-)

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now