怎么样将一个目录里的所有文件复制到另外一个目录,
在复制时检测目的目录中是否存在和源目录一样的文件,
如果有则不复制。

解决方案 »

  1.   

    uses shellAPI;
        procedure TForm1.Button1Click(Sender: TObject);
        var
           lpFileOp: TSHFileOpStruct;
        begin
          with lpFileOp do
          begin
            Wnd:=Self.Handle;
            wfunc:=FO_COPY;
            pFrom:=pchar('C:\AAA');
            pTo:=pchar('D:\AAA');
            fFlags:=FOF_ALLOWUNDO;
            hNameMappings:=nil;
            lpszProgressTitle:=nil;
            fAnyOperationsAborted:=True;
         end;
         if SHFileOperation(lpFileOp)<>0 then
         ShowMessage('删除失败');
        end;
      

  2.   

    我写的一个c++ builder程序,你可以参考一下
        AnsiString str;
        WIN32_FIND_DATA FindFileData;
        str=sourceDir.SubString(sourceDir.Length(),1);
        if(str==".")
          return true;
        if(str!="\\")sourceDir+="\\";
        str=sourceDir+AnsiString("*.*");
        HANDLE hFile=FindFirstFile(str.c_str(),&FindFileData);
        while(hFile)
        {
            str=sourceDir+FindFileData.cFileName;
    //        ShowMessage("Source File is :"+str);
            if(SubdirEnabled&&(FindFileData.dwFileAttributes&0x000000f0)==0x00000010||
                              (FindFileData.dwFileAttributes&0x000000f0)==0x00000030||
                              (FindFileData.dwFileAttributes&0x000000f0)==0x00000050||
                              (FindFileData.dwFileAttributes&0x000000f0)==0x00000070||
                              (FindFileData.dwFileAttributes&0x000000f0)==0x00000090)
            {
            if(SetCurrentDirectory(Edit1->Text.c_str()))
                    {
                    TakeFileName(str);
                    Dir=destDir+aaaa;
    //                ShowMessage("Dir is :"+Dir);
                    CreateDir(Dir);
                    }
            Copy_File(str,Edit2->Text,true);
            }
            else
            {                TakeFileName(str);
                   aa=destDir+aaaa;
    //               ShowMessage("DestFIle is:"+aa);
                    CopyFile(str.c_str(),aa.c_str(),true);        }        if(FindNextFile(hFile,&FindFileData)==false)
            {
                break;
            }
        }
        FindClose(hFile);
        return true;
      

  3.   

    楼上方法不错fAnyOperationsAborted:=True;//=FALSE为询问是否覆盖
      

  4.   

    : xiehua822的办法可以,但是存在问题;
    1,fAnyOperationsAborted:=True;也会询问;
    2,重要的是这个代码不能检测文件是否已经存在,而只是完全的将整个目录复制,原先目录已经存的文件也只是重新的复制一次。
    还有好的方法吗???
      

  5.   

    ---- 1、拷贝目录 ---- 为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。 ---- 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;---- 2、删除目录 ---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。 ---- 2.1删除目录的递归辅助函数:DoRemoveDir function DoRemoveDir(sDirName:String):Boolean;
    var
       hFindFile:Cardinal;
       tfile:String;
       sCurDir:String;
       bEmptyDir:Boolean;
       FindFileData:WIN32_FIND_DATA;
    begin
       //如果删除的是空目录,则置bEmptyDir为True
       //初始时,bEmptyDir为True
       bEmptyDir:=True;
       //先保存当前目录
       sCurDir:=GetCurrentDir;
       SetLength(sCurDir,Length(sCurDir));
       ChDir(sDirName);
       hFindFile:=FindFirstFile('*.*',FindFileData);
       if hFindFile< >INVALID_HANDLE_VALUE then
       begin
            repeat
                  tfile:=FindFileData.cFileName;
                  if (tfile='.') or (tfile='..') then
                  begin
                     bEmptyDir:=bEmptyDir and True;
                     Continue;
                  end;
                  //不是空目录,置bEmptyDir为False
                  bEmptyDir:=False;
                  if FindFileData.dwFileAttributes=
                  FILE_ATTRIBUTE_DIRECTORY then
                  begin
                       if sDirName[Length(sDirName)]< >'\' then
                          DoRemoveDir(sDirName+'\'+tfile)
                       else
                          DoRemoveDir(sDirName+tfile);
                       if not RemoveDirectory(PChar(tfile)) then
                          result:=false
                       else
                          result:=true;
                  end
                  else
                  begin
                       if not DeleteFile(PChar(tfile)) then
                          result:=false
                       else
                          result:=true;
                  end;
            until FindNextFile(hFindFile,FindFileData)=false;
            FindClose(hFindFile);
       end
       else
       begin
            ChDir(sCurDir);
            result:=false;
            exit;
       end;
       //如果是空目录,则删除该空目录
       if bEmptyDir then
       begin
            //返回上一级目录
            ChDir('..');
            //删除空目录
            RemoveDirectory(PChar(sDirName));
       end;   //回到原来的目录下
       ChDir(sCurDir);
       result:=true;
    end;---- 2.2删除目录的函数:DeleteDir function DeleteDir(sDirName:String):Boolean;
    begin
          if Length(sDirName)< =0 then
             exit;
          //删除...
          Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
    end;---- 3、移动目录 ---- 有了拷贝目录和删除目录的函数,移动目录就变得很简单,只需顺序调用前两个函数即可: function MoveDir(sDirName:String;
    sToDirName:string):Boolean;
    begin
         if CopyDir(sDirName,sToDirName) then
            if RemoveDir(sDirName) then
               result:=True
            else
               result:=false;
    end;///////////////////////////////////////////////
    procedure TForm1.Button2Click(Sender: TObject);
    var
      OpStruc: TSHFileOpStruct;
      frombuf, tobuf: Array [0..128] of Char;
    Begin
      FillChar( frombuf, Sizeof(frombuf), 0 );
      FillChar( tobuf, Sizeof(tobuf), 0 );
      StrPCopy( frombuf, 'd:\brief\*.*' );
      StrPCopy( tobuf, 'd:\temp\brief' );
      With OpStruc DO Begin
        Wnd:= Handle;
        wFunc:= FO_COPY;
        pFrom:= @frombuf;
        pTo:=@tobuf;
        fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
        fAnyOperationsAborted:= False;
        hNameMappings:= Nil;
        lpszProgressTitle:= Nil;  end;
      ShFileOperation( OpStruc );
    end;
      

  6.   

    : coreblood实在是感谢!!!
      

  7.   

    我是菜鸟用菜方法
    使用
     WinExec('cmd /k xcopy 目录1 目录2 /参数);
    搞定