FILE FUNCTIONS: is there a function to copy a whole folder?

Posted on 2003-02-21
Medium Priority
Last Modified: 2006-11-17

I was just wondering if there is a function to copy a whole folder...I know there is CopyFile.

BOOL CopyFile(
  LPCTSTR lpExistingFileName,
                          // pointer to name of an existing file
  LPCTSTR lpNewFileName,  // pointer to filename to copy to
  BOOL bFailIfExists      // flag for operation if file exists
But I can't find anything to copy a whole folder...

Thanks in Advance..

Question by:loudogz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 7993058
Yes there is, not sure if you like the answer though. Since I see from your post that you're in a windoze machine I'll give you the answer for windows:

system("xcopy /s...other options...args..");

You can also use CreateProcessEx() or something like that instead of system() but the net result is more or less the same.

Under unix the system("cp -r source... dest"); should work.

This was perhaps not the answer you were looking for but it does the job.

You can also use FindFirstFile()/FindNextFile() and copy the directory that way. Check the flag ISDIR for directory, if directory you copy it by recursively calling yourself to copy it. If it is a plain regular file you call CopyFile() to copy the file.

LVL 86

Accepted Solution

jkr earned 200 total points
ID: 7993194
Use 'SHFileOperation()', it is designed for that purpose:

    ZeroMemory  (   &sfo,   sizeof  (   SHFILEOPSTRUCT));

    sfo.wFunc   =   FO_COPY;
    sfo.pFrom   =   "c:\\path\\*.*";
    sfo.pTo     =   "c:\\anotherpath";

    if  (   SHFileOperation (   &sfo))
            // error

Author Comment

ID: 7995573
Thanks, You da "Man"

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

771 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