Delphi中拷贝文件夹的几个函数

 思路1:直接使用CopyFile,循环查找文件夹下文件,拷贝到指定目录,(这里测试,不含子目录)

procedure CopyDirectory(SourceDir, DesDir: string);
var
    DirInfo: TSearchRec;
    r: Integer;
begin
    r := FindFirst(SourceDir + '*.*', FaAnyFile, DirInfo);
    while r = 0 do
    begin
        if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
            (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
            CopyFile(pchar(SourceDir + DirInfo.Name), pchar(DesDir + DirInfo.Name), True);
        r := FindNext(DirInfo);
    end;
    SysUtils.FindClose(DirInfo);
end;
如果想含有子目录,加一递归即可.
思路2:使用API,TSHFileOpStruct 结构.含有子目录.
procedure CopyFileByFolder(Ahandle: THandle; fromDir,
  toDir: String);
var
  SHFileOpStruct: TSHFileOpStruct;
  pFromDir, pToDir: PAnsiChar;
begin
  GetMem(pFromDir, Length(fromDir)+2);
  try
    GetMem(pToDir, Length(toDir)+2);
    try

      FillChar(pFromDir^, Length(fromDir)+2, 0);
      FillChar(pToDir^, Length(toDir)+2, 0);

      StrCopy(pFromDir, PChar(fromDir));
      StrCopy(pToDir, PChar(toDir));

      with SHFileOpStruct do
      begin
        Wnd    := AHandle;   // Assign the window handle
        wFunc  := FO_COPY;  // Specify a file copy
        pFrom  := pFromDir;
        pTo    := pToDir;
        fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
        fAnyOperationsAborted := True;
        hNameMappings := nil;
        lpszProgressTitle := nil;
        if SHFileOperation(SHFileOpStruct) <> 0 then
          RaiseLastWin32Error;
      end;
    finally
      FreeMem(pToDir, Length(ToDir)+2);
    end;
  finally
    FreeMem(pFromDir, Length(FromDir)+2);
  end;
end;


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值