"patrickartaud" wrote in message
news:4773e6bd@newsgroups.borland.com...
> Hello,
> I would to copy directory and subdir with files they contain in another
> place.
> How can I do it the more simply?
> Because I try, but I arrive only copying step by step.
> Could you help me please?
> Thank you very much
Patrick,
Here's a function I wrote a *long* time ago. Note that the
'SourceDirectory' and 'TargetDirectory' must be fully qualified paths
(including the drive letter). I am sure I would write it differently today
than I did back then, but it should still work (as long as you give it full
paths):
bool CopyTree( char *SourceDirectory, char *TargetDirectory )
{
char *lpszOrigDir = 0;
int cchOrigDir = 0;
int cchReqd = 0;
bool success = true;
char SourceDir[MAXDIR]; // normalized existing directory path
char TargetDir[MAXDIR]; // normalized new directory path
// make sure the existing and new directory names are in the proper format
{
char drive[MAXDRIVE];
char dir[MAXDIR];
int contents;
// fnsplit requires directory names to end with a backslash
// if existing directory name does not end in backslash, append one
sprintf( SourceDir, "%s%c", SourceDirectory,
(SourceDirectory[strlen(SourceDirectory)-1] != '\\')?'\\':'\0' );
sprintf( TargetDir, "%s%c", TargetDirectory,
(TargetDirectory[strlen(TargetDirectory)-1] != '\\')?'\\':'\0' );
// directory names must contain drive and directory, but not filename or
extension
contents = fnsplit( SourceDir, drive, dir, NULL, NULL );
// make sure the source contains only path information
// (i.e. no filename or extension); abort otherwise
assert( !(contents & FILENAME) );
assert( !(contents & EXTENSION) );
if ( !(contents & DRIVE) || !(contents & DIRECTORY) )
{
SetLastError( ERROR_INVALID_NAME );
success = false;
goto Done;
}
}
// find out how many characters are required to save the current directory
cchReqd = GetCurrentDirectory( cchOrigDir, lpszOrigDir );
if ( !cchReqd )
{
success = false;
goto Done;
}
// add 1 for the NULL-terminator
cchOrigDir = cchReqd + 1;
// make space for the directory
lpszOrigDir = new char[cchOrigDir];
// save the current directory
cchReqd = GetCurrentDirectory( cchOrigDir, lpszOrigDir );
// now set the current directory to the source directory
if ( !SetCurrentDirectory( SourceDir ) )
{
success = false;
goto Done;
}
// create the new directory
if ( !CreateDirectory( TargetDir, NULL ) )
{
// it's OK if the directory already exists
if ( GetLastError() != ERROR_ALREADY_EXISTS )
{
success = false;
goto Done;
}
}
// now copy the files from the current directory to the new one
{
WIN32_FIND_DATA data; // information about the file/subdirectory found
HANDLE hFindFile; // search handle
// find the first file and create a handle for subsequent
files/subdirectories
hFindFile = FindFirstFile( "*.*", &data );
if ( hFindFile == INVALID_HANDLE_VALUE )
{
success = false;
goto Done;
}
do
{
char Source[MAXDIR];
char Target[MAXDIR];
// build a full path to the source and target files
sprintf( Source, "%s%s", SourceDir, data.cFileName );
sprintf( Target, "%s%s", TargetDir, data.cFileName );
// if we've found a subdirectory
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
// ignore current and previous directories
if ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName,
".." ) )
{
// append a trailing backslash to the path (required by
CopyTree())
strcat( Source, "\\" );
strcat( Target, "\\" );
// handle subdirectories recursively
if ( !CopyTree( Source, Target ) )
{
success = false;
goto Done;
}
}
}
// if we've found a file
else
{
// copy the file from the source to the target
if ( !CopyFile( Source, Target, false ) )
{
success = false;
goto Done;
}
}
// continue as long as there are more files/subdirectories
} while ( FindNextFile( hFindFile, &data ) );
// we're done with the handle
if ( !FindClose( hFindFile ) )
{
success = false;
goto Done;
}
}
Done:
if ( lpszOrigDir )
{
// restore the current directory
SetCurrentDirectory( lpszOrigDir );
// free the space used for the directory
delete [] lpszOrigDir;
}
return success;
}
- Dennis
|