Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying lots of files in Delphi

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;
like image 440
Charles Faiga Avatar asked Oct 19 '09 12:10

Charles Faiga


2 Answers

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.

like image 190
EagleOfToledo Avatar answered Nov 15 '22 08:11

EagleOfToledo


You can use the SHFileOperation() API call and use a wildcard in the file name of the struct. That way one call would be used to copy all of the files in one go. There's even the possibility to show the progress (via a callback function) and allow the user to cancel the operation.

like image 45
mghie Avatar answered Nov 15 '22 06:11

mghie