Copying lots of files in Delphi
delphi, delphi-2010
Solution
Definitely go with SHFileOperation() as suggested above, CopyFile is way too slow for that many files. It looks like you are basically restoring an entire folder so the search function may be unnecessary and slow things down further. Something like this may be of help:
uses ShellApi;
function CopyDir(const fromDir, toDir: string): Boolean;
var
fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do
begin
wFunc := FO_COPY;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;
This function will raise a prompt to overwrite existing files though (maybe it can be tweaked to skip that) but the user can select "All" so it's a one-click procedure, much faster, has a progress bar and can be canceled if desired.
Problem
In my application I need to copy over 1000 small files Here is the code I am using but it is VERY SLOW Is there a better way of doing this ? ``` procedure Tdatafeeds.RestotreTodaysFiles; var SearchRec: TSearchRec; FromFn, ToFn: string; Begin if DirectoryExists(BackupPath1) then begin try if FindFirst(BackupPath1 + '\*.*', (faAnyFile AND NOT(faDirectory)), SearchRec) = 0 then begin repeat FromFn := BackupPath1 + '\' + SearchRec.name; ToFn := DatafeedsPath1 + '\' + SearchRec.name; CopyFile(Pchar(FromFn), Pchar(ToFn), false); until FindNext(SearchRec) <> 0; end; finally FindClose(SearchRec); end; end; End; ```