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 给动态数组添加一个元素

Delphi xe安装后配置android的sdk的方法

Delphi 获取中文/数字星期的函数

Delphi 关于多语言:如何获取当前系统语言

settimer函数用法

Delphi repeat until 运用

Delphi listview1 中添加check选中事件

Delphi 提取字符中的数字

Delphi+access错误"不正常地定义参数对象。提供了不一致或不完整的信息。"

Delphi 根据扩展名的文件搜索

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



打赏

取消

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

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

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

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

评论

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