谁能帮我写一个拷贝目录(包括其子目录)的函数???

解决方案 »

  1.   

    复制目录:
    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;删除目录:function DelDirectory(const Source:string): boolean;
    var
      fo: TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
      begin
        Wnd := 0;
        wFunc := FO_DELETE;
        pFrom := PChar(source+#0);
        pTo := #0#0;
        fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
      end;
      Result := (SHFileOperation(fo) = 0);
    end;重新命名:
    用MoveFile()或者下面的函数也可以。
    RenameFile('c:\a','c:\b')好想也可以?Win2K。
    //RenDirectory('d:\wt2','d:\bcde');
    function RenDirectory(const OldName,NewName:string): boolean;
    var
      fo: TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
      begin
        Wnd := 0;
        wFunc := FO_RENAME;
        pFrom := PChar(OldName+#0);
        pTo := pchar(NewName+#0);
        fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
      end;
      Result := (SHFileOperation(fo) = 0);
    end;
    //Copy 多个文件的处理:
    function CopyFiles(const Source,Dest: string): boolean;
    var
      fo: TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
      begin
        Wnd := 0;
        wFunc := FO_COPY;
        pFrom := @source[1];
        pTo :=pchar(dest);
        fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR    ;
      end;
      Result := (SHFileOperation(fo) = 0);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      str:string;
      i:integer;
    begin
      if opendialog1.Execute then
      begin
        for i:=0 to OpenDialog1.Files.Count-1 do
         str:=str+OpenDialog1.Files.strings[i]+#0;
        showmessage(str);
        str:=str+#0;
        CopyFiles(str,'d:\temp');
      end;
    end;
      

  2.   

    ---- 1.1拷贝目录的递归辅助函数:DoCopyDir 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;---- 1.2拷贝目录的函数:CopyDir function CopyDir(sDirName:String;
    sToDirName:string):Boolean;
    begin
          if Length(sDirName)< =0 then
             exit;
          //拷贝...
          Result:=DoCopyDir(sDirName,sToDirName);
    end;