Copying a Directory
Posted: Thu Apr 28, 2005 11:01 pm
Per our previous discussion over at: http://forums.wxwidgets.org/viewtopic.php?t=1833 I finally got around to laying down the copy dir function. I think it's pretty handy. Thanks for the input on it. I incorporated the ideas from the thread. Please edit it if you think it's bloated, and if it's not usefull to anyone then my appologies for the following graffiti:
Code: Select all
// =================================================================
// Date: 04/28/05
// Author: ... doesn't matter ...
// Platforms: Written and tested under Win XP
// Compiler: MSVC++ 6.0
// wxWidgets: Tested under 2.6.0, but should work in others.
//
// Description: Copies one directory contents to another,
// creating the destination directory if it does not exist. I'm sure
// there might be better ways to do this, but this should work.
//
// Resources: Original discussion at:
// http://wxforum.shadonet.com/viewtopic.php?t=1833
//
// Example Usage:
//
// CopyDir("C:\\dev\\test", "c:\\devCopy\\testCopy");
// =================================================================
bool CopyDir(wxString from, wxString to) {
// set up our proper directory slash character. If running on
// windows, __WXMSW__ should be defined in the preprocessor definitions!
// this is just how i handle slashes. anyone know a more efficient way?
#ifdef __WXMSW__
wxString SLASH = "\\";
#else
wxString SLASH = "/";
#endif
// append a slash if there is not one (for easier parsing)
// because who knows what people will pass to the function.
if (to[to.length()-1] != SLASH) {
to += SLASH;
}
// for both dirs
if (from[from.length()-1] != SLASH) {
from += SLASH;
}
// first make sure that the source dir exists
if(!wxDir::Exists(from)) {
wxLogError(from + " does not exist. Can not copy directory.");
}
else {
// check on the destination dir
// if it doesn't exist...
if(!wxDir::Exists(to)) {
// if it doesn't get created
if(!wxFileName::Mkdir(to, 0777, wxPATH_MKDIR_FULL)) {
// Send an error
wxLogError(to + " could not be created.");
// And exit gracefully
return false;
}
}
// The directories to traverse
wxArrayString myDirs;
// Initially we want one pass
myDirs.Add("");
// loop through each directory.. storing all sub directories
// and copying over all files.. the final iteration of one loop
// should begin an iteration for any subdirectories discovered
// on the previous pass
// (rather than pragma, unsigned int will shut the MS compiler up)
for (unsigned int i = 0; i < myDirs.size(); i++) {
// get the next directory
wxDir nextDir(from + myDirs[i]);
// check that it exists in destination form
if(!wxDir::Exists(to + myDirs[i])) {
// if it doesn't, then create it
if(!wxFileName::Mkdir(to + myDirs[i], 0777, wxPATH_MKDIR_FULL)) {
// If it doesn't create, error
wxLogError(to + myDirs[i] + " could not be created.");
// And exit gracefully
return false;
}
}
// get the first file in the next directory
wxString nextFile;
bool process = nextDir.GetFirst(&nextFile);
// and while there are still files to process
while (process) {
// If this file is a directory
if(wxDir::Exists(from+nextFile)) {
// then append it for creation/copying
myDirs.Add(nextFile + SLASH); // only add the difference
}
else {
// otherwise just go ahead and copy the file over
if(!wxCopyFile(from + myDirs[i] + nextFile,
to + myDirs[i] + nextFile)) {
// error if we couldn't
wxLogError("Could not copy " +
from + myDirs[i] + nextFile + " to "
+ to + myDirs[i] + nextFile);
}
}
// and get the next file
process = nextDir.GetNext(&nextFile);
}
}
return true;
}
return false;
}
// =================================================================