比如我想把d:\temp文件夹拷贝到e:\mytemp\下,程序如何做到

解决方案 »

  1.   

    //下面两个函数为文件拷贝函数,带有路径的,参数是源路径和目标路径:
    //拷贝目录的递归辅助函数:DoCopyDir
    function SelfCopyDir(sDirName:String;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;
                    CopyFile(PChar(tfile),PChar(t),false);
                end;
            until FindNextFile(F.FindHandle,FindFileData)=false;
               FindClose(F);
            end
            else
            begin
                ChDir(sCurDir);
                result:=false;
                exit;
            end;
            //回到原来的目录下
            ChDir(sCurDir);
            result:=true;
        end;//拷贝目录的函数:CopyDir
    function CopyDir(sDirName:String;sToDirName:string):Boolean;
    begin
       if Length(sDirName)<=0 then
          exit;
            //拷贝...
       Result:=SelfCopyDir(sDirName,sToDirName);
    end;