2010. szeptember 5., vasárnap

How to copy all files from one directory to another (2)


Problem/Question/Abstract:

I would like to copy my work directory to H:\Backups and rename it to today's date, eg: H:\Backups\030801\

Answer:

procedure CopyTree(dir, dest: string);
var
  sfos: TSHFileOpStruct;
begin
  FillChar(sfos, SizeOf(sfos), 0);
  dir := dir + '\*.*'#0;
  dest := dest + '\*.*'#0;
  with sfos do
  begin
    wnd := 0;
    wfunc := FO_COPY;
    pFrom := PChar(dir);
    pTo := PChar(dest);
    fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION or FOF_SIMPLEPROGRESS;
    fAnyOperationsAborted := false;
    hNameMappings := nil;
    lpszProgressTitle := nil
  end;
  SHFileOperation(sfos)
end;


Create the destination directory with MkDir or ForceDirectories and call


CopyTree('c:\workdir', 'h:\backup\030801');


This will copy eventual subdirectories of c:\workdir too.

1 megjegyzés: