有两个文件夹,A/B,A在远程,B在本地,文件夹B里面的内容要和A里面的一样,当然可以通过拷贝文件夹的方式来使它们内容一样,但这样只是对整个文件夹的完全拷贝,如果B里只少了1,2个文件,那完全拷贝一次就是浪费时间和资源了!不知道有没有什么方法可以做到只对缺少的文件进行复制,其实有点像现在的网络游戏的自动更新!

解决方案 »

  1.   

    放一个button1上去将button1.click下copydir('源目录','目的目录');改成你的就行了.
    unit copydirr;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,filectrl;type
      Tcopyform = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      copyform: Tcopyform;
      ficount:integer;
      function DoCopyDir(sDirName:String;sToDirName:String):Boolean;
    function CopyDir(sDirName:String;sToDirName:string):Boolean;
    function MoveDir(sDirName:String;
    sToDirName:string):Boolean;
    function dispw(const sdirname,tfile,t:string):integer;
    implementationuses filetree;{$R *.DFM}procedure Tcopyform.Button1Click(Sender: TObject);
    begin
    copydir('c:\drv','\\remote\drv1');
    end;function DoCopyDir(sDirName:String;
    sToDirName:String):Boolean;
    var
          hFindFile:Cardinal;
      //   hfindfile: TSearchRec;
       t,tfile:String;
       sCurDir:String[255];
       FindFileData:WIN32_FIND_DATA;
       fidate:tdatetime;
    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;
                     
                       ficount:=ficount+1;
                       if pub_datesel=1 then begin
                          if not fileexists(t) then
                           dispw(sdirname,tfile,t)
                          else begin
                          fidate:=filedatetodatetime(fileage(t));
                          if fidate>=pub_date then
                          dispw(sdirname,tfile,t);
                          end;
                       end else
                                            
                                    end;
            until FindNextFile(hFindFile,FindFileData)=false;
          // FindClose(hFindFile);
       end
       else
       begin
            ChDir(sCurDir);
            result:=false;
            exit;
       end;
       //回到原来的目录下
       ChDir(sCurDir);
       result:=true;
    end;
    //---- 1.2拷贝目录的函数:CopyDirfunction CopyDir(sDirName:String;
    sToDirName:string):Boolean;
    begin
          if Length(sDirName)<=0 then
             exit;
          //拷贝...
          Result:=DoCopyDir(sDirName,sToDirName);
    end;
    function MoveDir(sDirName:String;
    sToDirName:string):Boolean;
    begin
         if CopyDir(sDirName,sToDirName) then
            if RemoveDir(sDirName) then
               result:=True
            else
               result:=false;
    end;