一个关于文件夹复制的问题(100分)

  • 主题发起人 主题发起人 葡萄
  • 开始时间 开始时间

葡萄

Unregistered / Unconfirmed
GUEST, unregistred user!
我用SHFileOperation做了一个目录拷贝函数,但是在拷贝过程中如果遇到了无法复制的文件,程序就终止了,我想能不能跳过无法复制的文件,让程序继续执行,程序如下:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;FileOpStruct :TSHFileOpStruct;<br>&nbsp;Buf1 :array [0..127] of Char;<br>&nbsp;Buf2 :array [0..127] of Char;<br>&nbsp;Str1,Str2 :String;<br>begin<br>&nbsp;Str1 :=edit2.Text;<br>&nbsp;Str2 :=edit1.Text;<br>&nbsp;FillChar(Buf1,SizeOf(Buf1),0);<br>&nbsp;FillChar(Buf2,SizeOf(Buf2),0);<br>&nbsp;StrPCopy(Buf1,Str1);<br>&nbsp;StrPCopy(Buf2,Str2);<br>&nbsp;with FileOpStruct do<br>&nbsp;begin<br>&nbsp; &nbsp;Wnd:=Handle;<br>&nbsp; &nbsp;wFunc:=FO_COPY;<br>&nbsp; &nbsp;pFrom:=@Buf1;<br>&nbsp; &nbsp;pTo:=@Buf2;<br>&nbsp; &nbsp;fFlags:=FOF_ALLOWUNDO;<br>&nbsp; &nbsp;fAnyOperationsAborted:=False;<br>&nbsp; &nbsp;hNameMappings:=nil;<br>&nbsp; &nbsp;lpszProgressTitle:='拷贝文件';<br>&nbsp;end;<br>&nbsp;SHFileOperation(FileOpStruct);<br>end;<br><br>end.
 
试下try finally end<br>或用时间控件!
 
用try即可<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;FileOpStruct :TSHFileOpStruct;<br>&nbsp;Buf1 :array [0..127] of Char;<br>&nbsp;Buf2 :array [0..127] of Char;<br>&nbsp;Str1,Str2 :String;<br>begin<br>&nbsp;Str1 :=edit2.Text;<br>&nbsp;Str2 :=edit1.Text;<br>&nbsp;FillChar(Buf1,SizeOf(Buf1),0);<br>&nbsp;FillChar(Buf2,SizeOf(Buf2),0);<br>&nbsp;StrPCopy(Buf1,Str1);<br>&nbsp;StrPCopy(Buf2,Str2);<br><br>try<br><br>&nbsp;with FileOpStruct do<br>&nbsp;begin<br>&nbsp; &nbsp;Wnd:=Handle;<br>&nbsp; &nbsp;wFunc:=FO_COPY;<br>&nbsp; &nbsp;pFrom:=@Buf1;<br>&nbsp; &nbsp;pTo:=@Buf2;<br>&nbsp; &nbsp;fFlags:=FOF_ALLOWUNDO;<br>&nbsp; &nbsp;fAnyOperationsAborted:=False;<br>&nbsp; &nbsp;hNameMappings:=nil;<br>&nbsp; &nbsp;lpszProgressTitle:='拷贝文件';<br>&nbsp;end;<br>&nbsp;SHFileOperation(FileOpStruct);<br><br>except<br>end;<br><br>end;<br><br>end.
 
下面的东东不知道对你有没有帮助:<br>1、拷贝目录 <br><br>为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。 <br><br>1.1拷贝目录的递归辅助函数:DoCopyDir <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>1.2拷贝目录的函数:CopyDir <br><br>function CopyDir(sDirName:String;<br>sToDirName:string):Boolean;<br>begin<br>&nbsp; &nbsp; &nbsp; if Length(sDirName)&lt; =0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; //拷贝...<br>&nbsp; &nbsp; &nbsp; Result:=DoCopyDir(sDirName,sToDirName);<br>end;<br>
 
楼上的朋友,你这个函数我也试过,但是无法拷贝隐藏文件夹~~
 
yue_shan 非常感谢你,但是按照你的写法,程序并没有跳过不能复制的程序,而是整个程序都终止了~~
 
把那句话修改一下,或许可以。<br>if FindFileData.dwFileAttributes=<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_DIRECTORY then<br>改成<br>if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) =FILE_ATTRIBUTE_DIRECTORY then
 
非常感谢realLearning的帮助,问题差不多解决了,同时也感谢其他朋友,开始分分了~~
 
多人接受答案了。
 
后退
顶部