枚举指定路径下文件及文件夹复制到指定路径下

本文介绍了一个用于递归复制文件夹的函数实现,包括文件和子目录的完整复制过程。该函数首先检查目标目录是否存在,若不存在则创建之,随后遍历源目录中的所有文件和子目录,并将它们复制到目标位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function   DoCopyDir(sDirName,   sToDirName:   String):   Boolean;
var
   F:   TSearchRec;
   //hFindFile:Cardinal;
   t,tfile:String;
   sCurDir:String[255];
   FindFileData:WIN32_FIND_DATA;
begin
   //先保存当前目录
   sCurDir:=GetCurrentDir;
   ChDir(sDirName);
   F.FindHandle:=FindFirstFile('*.*',FindFileData);
   if   F.FindHandle<>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;
            Form1.StatusBar1.Panels[0].Text   :=  t;
            Form1.StatusBar1.Refresh;
            //Frmmain.msgWriter('正在拷贝:     '+t);
            CopyFile(PChar(tfile),PChar(t),false);
            //     FrmMain.Prgb.StepBy(1);
         end;
      until FindNextFile(F.FindHandle,FindFileData)=false;
            FindClose(F);
      end else
      begin
         ChDir(sCurDir);
         result:=false;
         exit;
   end;
   //回到原来的目录下
   ChDir(sCurDir);
   result:=true;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值