Delphi复制文件夹内所有文件


本文整理自网络,侵删。

 
function DoCopyDir(sDirName: string; sToDirName: string): Boolean;
var
  hFindFile: Cardinal;
  t, tfile: string;
  sCurDir: string[255];
  FindFileData: WIN32_FIND_DATA;
begin
//记录当前目录
  sCurDir := GetCurrentDir;
  ChDir(sDirName);
  hFindFile := FindFirstFile('*.*', FindFileData);
  if hFindFile <> INVALID_HANDLE_VALUE then
  begin
    if not DirectoryExists(sToDirName) then
      ForceDirectories(sToDirName);
    repeat
      tfile := FindFileData.cFileName;
      if (tfile = '.') or (tfile = '..') then
        Continue;
      if FindFileData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then
      begin
        t := sToDirName + '\' + tfile;
        if not DirectoryExists(t) then
          ForceDirectories(t);
        if sDirName[Length(sDirName)] <> '\' then
          DoCopyDir(sDirName + '\' + tfile, t)
        else
          DoCopyDir(sDirName + tfile, sToDirName + tfile);
      end
      else
      begin
        t := sToDirName + '\' + tFile;
        CopyFile(PChar(tfile), PChar(t), True);
      end;
    until FindNextFile(hFindFile, FindFileData) = false;
///     FindClose(hFindFile);
  end
  else
  begin
    ChDir(sCurDir);
    result := false;
    exit;
  end;
//回到当前目录
  ChDir(sCurDir);
  result := true;
end;

===============================================
第二种方法

uses ShellApi;

///复制Source整个目录到DEST目录,如果Dest不存在,自动建立,如果DEST存在,那么Source将作为Dest的子目录!  
  //例如如果要复制E:\Temp整个目录到E:\那么代码为:   copydirectory('e:\temp','e:\');  
  ///如果要复制E:\Temp到E:\Test目录下面,那么代码为:CopyDirecotry('E:\Temp','E:\TEST');  
  function   CopyDirectory(const   Source,   Dest:   string):   boolean;  
  var  
      fo:   TSHFILEOPSTRUCT;  
  begin  
      FillChar(fo,   SizeOf(fo),   0);  
      with   fo   do  
      begin  
          Wnd   :=   0;  
          wFunc   :=   FO_COPY;  
          pFrom   :=   PChar(source+#0);  
          pTo   :=   PChar(Dest+#0);  
          fFlags   :=   FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR         ;  
      end;  
      Result   :=   (SHFileOperation(fo)   =   0);  
  end;  

相关阅读 >>

Delphi xe取得硬盘序列号代码 复制代码

Delphi dbnavigator控件的按钮显示成中文

Delphi 提权,杀进程,删服务

Delphi 打包文件到apk安装包中

Delphi access violations 问题的解决之道

Delphi winapi: getfocus - 获取当前拥有焦点的窗口的句柄

Delphi二值图像膨胀算法

Delphi firedac 如何按整型(byte)读取 mysql tinyint(1) 类型字段?

Delphi richedit选中文字右键菜单的实现

Delphi 数字分隔

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...