delphi 复制文件夹的函数

procedure ShellCopy(const dir1: string; const dir2: string); /////格式shellcopy('d:\1*.*','e:\1\');///windows外壳拷贝
var
  f: TSearchRec;
  na, sdir: string;
  s: TStringList;
  i: integer;
  fshell: TSHFileOpStruct;
begin
  s := TStringList.Create;
  //////////////////////
  if (not DirectoryExists(dir2)) then CreateDir(dir2);
  sdir := dir1;
  Delete(sdir, length(sdir) - 2, 3);
  if FindFirst(dir1, faAnyFile, f) = 0 then
  begin
    if (f.Name <> 'database.dat') and (f.Attr <> faDirectory) then
    begin
      s.Add(f.Name);
    end;
    repeat
      na := f.Name;
      FindNext(f);
      if (na <> f.Name) and (f.Name <> 'database.dat') and (f.Attr <> faDirectory) then
      begin
        s.Add(f.Name);
      end;
    until na = f.Name;
  end;
  //////////////////////
  fshell.Wnd := form1.Handle;
  fshell.wFunc := FO_COPY;
  fshell.fFlags := FOF_SILENT;
  for i := 0 to s.Count - 1 do
  begin
    fshell.pFrom := pansichar(sdir + s.Strings[i]);
    fshell.pTo := pansichar(dir2 + s.strings[i]);
    SHFileOperation(fshell);
  end;
end;

发表回复

登录... 后才能评论