How to copy entire folder structure from C++?

Posted on 1999-01-19
Last Modified: 2013-12-03
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.
Question by:shrif

Expert Comment

ID: 1419019

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 if you are interested.


Expert Comment

ID: 1419020
How about using the DOS command xcopy with the /s option?
You can fire it off from your c++ code using the CreateProcess API.
LVL 86

Expert Comment

ID: 1419021
Hmm - this can be done much easier, 'SHFileOperation()' should do the job...
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 1419022

Expert Comment

ID: 1419023
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(* !='.')

                  //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 );



Author Comment

ID: 1419024
jkr, repost as an 'answer'

LVL 86

Accepted Solution

jkr earned 100 total points
ID: 1419025
Thanx shrif ;-)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question