delphi复制文件夹内所有文件  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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;  

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.035727977752686 seconds