拷贝整个目录,包含子目录(200分)

  • 主题发起人 主题发起人 pengjinlong
  • 开始时间 开始时间
P

pengjinlong

Unregistered / Unconfirmed
GUEST, unregistred user!
我用API函数shfileoperation
代码:
作了个小程序,可拷贝整个目录,但目录中不能含子目录,否则出错,<br>怎样才能拷含子目录的整个目录
 
用FileCopy+递归的方法不行吗?我就是这样做的。
 
我用时没出现这种情况
 
是不是要源代码,出两百元这么多
 
Delphi自带的目录操作函数不能复制含有子目录的目录,所以要自己动手<br><br>function DoCopyDir(sDirName:String; <br>sToDirName:String):Boolean; <br>var <br>&nbsp; &nbsp;hFindFile:Cardinal; <br>&nbsp; &nbsp;t,tfile:String; <br>&nbsp; &nbsp;sCurDir:String[255]; <br>&nbsp; &nbsp;FindFileData:WIN32_FIND_DATA; <br>begin <br>&nbsp; &nbsp;//先保存当前目录 <br>&nbsp; &nbsp;sCurDir:=GetCurrentDir; <br>&nbsp; &nbsp;ChDir(sDirName); <br>&nbsp; &nbsp;hFindFile:=FindFirstFile('*.*',FindFileData); <br>&nbsp; &nbsp;if hFindFile&lt; &gt;INVALID_HANDLE_VALUE then <br>&nbsp; &nbsp;begin <br>&nbsp; &nbsp; &nbsp; &nbsp; if not DirectoryExists(sToDirName) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ForceDirectories(sToDirName); <br>&nbsp; &nbsp; &nbsp; &nbsp; repeat <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tfile:=FindFileData.cFileName; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tfile='.') or (tfile='..') then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FindFileData.dwFileAttributes= <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_ATTRIBUTE_DIRECTORY then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t:=sToDirName+'/'+tfile; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if &nbsp;not DirectoryExists(t) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ForceDirectories(t); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if sDirName[Length(sDirName)]&lt; &gt;'/' then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoCopyDir(sDirName+'/'+tfile,t) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoCopyDir(sDirName+tfile,sToDirName+tfile); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t:=sToDirName+'/'+tFile; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CopyFile(PChar(tfile),PChar(t),True); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; until FindNextFile(hFindFile,FindFileData)=false; <br>&nbsp; &nbsp; &nbsp; &nbsp; FindClose(hFindFile); <br>&nbsp; &nbsp;end <br>&nbsp; &nbsp;else <br>&nbsp; &nbsp;begin <br>&nbsp; &nbsp; &nbsp; &nbsp; ChDir(sCurDir); <br>&nbsp; &nbsp; &nbsp; &nbsp; result:=false; <br>&nbsp; &nbsp; &nbsp; &nbsp; exit; <br>&nbsp; &nbsp;end; <br>&nbsp; &nbsp;//回到原来的目录下 <br>&nbsp; &nbsp;ChDir(sCurDir); <br>&nbsp; &nbsp;result:=true; <br>end; <br><br>function CopyDir(sDirName:String; <br>sToDirName:string):Boolean; <br>begin <br>&nbsp; &nbsp; &nbsp; if Length(sDirName)&lt; =0 then <br>exit; <br>//拷贝... <br>Result:=DoCopyDir(sDirName,sToDirName); <br>end; <br><br>这样利用CopyDir函数即可实现任意目录的拷贝了!<br>
 
我的下面一段代码是经过测试的,可以连子目录拷贝:<br>var<br>&nbsp; OpStruc: TSHFileOpStruct;<br>&nbsp; FromBuf, ToBuf: Array [0..128] of Char;<br>begin<br>&nbsp; FillChar( FromBuf, Sizeof(FromBuf), 0 );<br>&nbsp; FillChar( ToBuf, Sizeof(ToBuf), 0 );<br>&nbsp; StrPCopy( FromBuf, Pchar(Edit1.Text) ); //edit1的内容是源目录<br>&nbsp; StrPCopy( ToBuf, Pchar(Edit2.Text) ); &nbsp;//edit2是目的目录<br>&nbsp; // 设置OpStruc<br>&nbsp; with OpStruc do<br>&nbsp; begin<br>&nbsp; &nbsp; Wnd:= Handle;<br>&nbsp; &nbsp; wFunc:= FO_COPY;<br>&nbsp; &nbsp; pFrom:= @FromBuf;<br>&nbsp; &nbsp; pTo := @ToBuf ;<br>&nbsp; &nbsp; fFlags:=FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;<br>&nbsp; &nbsp; fAnyOperationsAborted:= False;<br>&nbsp; &nbsp; hNameMappings:= nil;<br>&nbsp; &nbsp; lpszProgressTitle:=nil;<br>&nbsp; end;<br>&nbsp; if SHFileOperation( OpStruc ) = 0 then &nbsp;//复制成功返回值为0<br>&nbsp; MessageBox(Handle,'复制完毕。' , '信息' ,Mb_Ok+Mb_IconInformation);<br>end;<br>
 
arnew的方法我常用的,呵呵,好用<br>而且稍加修改可以变成doMoveDir、doUploadDir等等
 
HunterTeam的方法简单明了。不用循环。推荐!
 

Similar threads

D
回复
0
查看
887
DelphiTeacher的专栏
D
D
回复
0
查看
855
DelphiTeacher的专栏
D
D
回复
0
查看
806
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部