Is there any way to browse and recursively copy/move all files and subdirectories of a directory within the code section? (PrepareToInstall
)
I need to ignore a specific directory, but using xcopy
it ignores all directories /default/
, for example, and I need to ignore a specific only.
The Files
section is executed at a later time when needed.
To copy a single file or multiple files recursively with the Windows command prompt, use the xcopy command.
Method 1: Using shutil. Using copyfile() method of shutil library we can easily copy a file from one location to other location. It takes 2 arguments the source path where the file that needs to be copied exist and the destination path where file is needed to be copied.
Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.
To recursively copy a directory programmatically use:
procedure DirectoryCopy(SourcePath, DestPath: string);
var
FindRec: TFindRec;
SourceFilePath: string;
DestFilePath: string;
begin
if FindFirst(SourcePath + '\*', FindRec) then
begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
begin
SourceFilePath := SourcePath + '\' + FindRec.Name;
DestFilePath := DestPath + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
begin
if FileCopy(SourceFilePath, DestFilePath, False) then
begin
Log(Format('Copied %s to %s', [SourceFilePath, DestFilePath]));
end
else
begin
Log(Format('Failed to copy %s to %s', [
SourceFilePath, DestFilePath]));
end;
end
else
begin
if DirExists(DestFilePath) or CreateDir(DestFilePath) then
begin
Log(Format('Created %s', [DestFilePath]));
DirectoryCopy(SourceFilePath, DestFilePath);
end
else
begin
Log(Format('Failed to create %s', [DestFilePath]));
end;
end;
end;
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end
else
begin
Log(Format('Failed to list %s', [SourcePath]));
end;
end;
Add any filtering you need. See how the .
and ..
are filtered.
Note that the function does not create the root DestPath
. If you do not know if it exists, add this to be beginning of the code:
if DirExists(DestPath) or CreateDir(DestPath) then
(then the similar code before the recursive DirectoryCopy
call becomes redundant)
For an example of use, see my answers to questions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With