Click to See Complete Forum and Search --> : Folder copying


Johan D'Hondt
May 18th, 1999, 07:15 AM
Hello everybody,

can somebody give me any hints on how to perform copy operations off complete folders (and subfolders) to another location by using VC++ commands?
(I am using VC++ 4.2)

Thanks,
Johan

olivier
May 18th, 1999, 08:15 AM
Use FindFirstFile to list all the files of the directory
then do a loop with FindNextFile and CopyFile

You'll need also CreateDirectory

Johan M
May 19th, 1999, 02:32 AM
Below is a program which is a modification of a method I have used recently.
It works under Visual C++ 6.0 and Windows NT 4.0, but I believe it works
in other machine configurations too.

Note that there is no error or exception handling (and I'm a lousy programmer). :-)

/Johan


// "foldername" is your source folder, dest_name is the root to the destination.
// This algorithm searches "depth-first", which perhaps is not the most efficient

void YourClass::copy_folder(CString *foldername, CString *dest_name)
{
struct _finddata_t buffer;
CString b_name;
long handle;

if (_chdir(*foldername) != -1L) {
handle = _findfirst("*", &buffer);
if (handle != -1L)
do { // do this if atlest one file is present
b_name = buffer.name;
if (b_name != "." && b_name != "..") {
if ((buffer.attrib & _A_SUBDIR) != _A_SUBDIR)
// Copy the file here to dest_name
else {
// It was a subdirectory, create a new subdirectory here at dest_name
// Do a recursive call below to extract the subfolder
copy_folder(&(*foldername + "/" + b_name), &(*dest_name + "/" + b_name));
}
}
}
while (_findnext(handle, &buffer) == 0);
}
}