// JP opened flex table

Click to See Complete Forum and Search --> : copy a directory


phreeezsix
January 4th, 2003, 03:06 PM
Hi, I really need to copy a directory in my program, and I have found at least 20 examples of how to do this, but every single one uses MFC. I am using a Borland compiler, so I need to do it using the standard win32 API. If anyone has an example, knows of a site to visit, or can point me in the right direction, I would really appreciate it. Thank you!

Tom

poccil
January 4th, 2003, 03:22 PM
Copying a directory involves enumerating all the files and folders within the source folder, and creating the directory tree in the destination. The function to copy the directory must be recursive, since directories within directories may need to be copied.



From another thread:

Unfortunately, there is no "easy" way to copy an entire subdirectory to another place in Windows.
This is what you do:



Call CreateDirectory to create a new directory in a new location.

Call FindFirstFile and FindNextFile to cycle through all the files in the original directory.

Call MoveFile to move all the files from the old directory into the new one.

Call RemoveDirectory to remove the old directory.
Don't forget to use FindClose to close the search handle instead of CloseHandle.

Click here: http://hsj.shadowpenguin.org/misc/otfw_c.txt

phreeezsix
January 4th, 2003, 03:36 PM
Thanx for the suggestion, but the code from that site isn't working for me. It compiles fine, but the directory isn't getting copied. Have u successfully used it? I didn't want to write my own method to do this bcz it's really re-inventing the wheel. If you haven't actually used that link u gave me, do u know of ne code that words for sure? thanx

Tom

TheCPUWizard
January 4th, 2003, 04:16 PM
An alternative (do a search for existing threads discussing the advandages and liabilities) is to spawn "xcopy".

phreeezsix
January 4th, 2003, 04:47 PM
Thanx for the suggestion, but the code from that site isn't working for me. It compiles fine, but the directory isn't getting copied. Have u successfully used it? I didn't want to write my own method to do this bcz it's really re-inventing the wheel. If you haven't actually used that link u gave me, do u know of ne code that words for sure? thanx

Tom

phreeezsix
January 4th, 2003, 04:48 PM
sorry about that double post. will check out xcopy.

Andreas Masur
January 4th, 2003, 04:53 PM
Originally posted by phreeezsix
Thanx for the suggestion, but the code from that site isn't working for me. It compiles fine, but the directory isn't getting copied. Have u successfully used it? I didn't want to write my own method to do this bcz it's really re-inventing the wheel. If you haven't actually used that link u gave me, do u know of ne code that words for sure? thanx

Tom
Well...I did not take a look neither at the mentioned site nor at the mentioned code.

But 'MoveFile()' will move the directory rather than copy it...

The following function should do the trick...

#include <string>
#include <iostream>

#include <conio.h>


int CopyDirectory(const std::string &refcstrSourceDirectory,
const std::string &refcstrDestinationDirectory)
{
std::string strSource; // Source file
std::string strDestination; // Destination file
std::string strPattern; // Pattern
HANDLE hFile; // Handle to file
WIN32_FIND_DATA FileInformation; // File information


strPattern = refcstrSourceDirectory + "\\*.*";

// Create destination directory
if(::CreateDirectory(refstrDestinationDirectory.c_str(), 0) == FALSE)
return ::GetLastError();

hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(FileInformation.cFileName[0]; != '.')
{
strSource.erase();
strDestination.erase();

strSource = refcstrSourceDirectory + "\\" + FileInformation.cFileName;
strDestination = refcstrDestinationDirectory + \\ + FileInformation.cFileName;

if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// Copy subdirectory
if(CopyDirectory(strSource, strDestination))
return iRC;
}
else
{
// Copy file
if(::CopyFile(strSource.c_str(), strDestination.c_str(), TRUE) == FALSE)
return ::GetLastError();
}
}
} while(::FindNextFile(hFile, &FileInformation) == TRUE);

// Close handle
::FindClose(hFile);

DWORD dwError = ::GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
return dwError;
}

return 0;
}

int main()
{
// Copy 'c:\mydir' to 'd:\mydir'
int iRC = CopyDirectory("c:\\mydir", "d:\\mydir");
if(iRC)
{
std::cout << "Error " << iRC << std::endl;
return -1;
}

// Wait for keystroke
_getch();

return 0;
}

I wrote it just down...so it might not even compile or work... :cool:

DanM
January 5th, 2003, 12:33 PM
There is an easier way: use SHFileOperation and set lpFileOp.wFunc to FO_COPY or FO_MOVE depending on what you really need.

SHFileOperation Function
Copies, moves, renames, or deletes a file system object.

int SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shfileoperation.asp

Dan

Steve_XP
January 7th, 2003, 08:56 AM
Andreas Masur, good source, you're always best

//JP added flex table