请问拷贝目录的API是什么?(50分)

  • 主题发起人 主题发起人 yanyandt2
  • 开始时间 开始时间
来自:Fyx, 时间:2000-11-18 09:11:00, ID:397396 <br>------------------------------------------------------------------<br>以下代码可以把C盘下的文件夹a复制成文件夹b: <br>uses ..., ShellAPI <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>var <br>OpStruc: TSHFileOpStruct; <br>frombuf, tobuf: Array [0..128] of Char; <br>Begin <br>FillChar( frombuf, Sizeof(frombuf), 0 ); <br>FillChar( tobuf, Sizeof(tobuf), 0 ); <br>StrPCopy( frombuf, 'c:/a' ); <br>StrPCopy( tobuf, 'c:/b' ); <br>With OpStruc DO Begin <br>Wnd:= Handle; <br>wFunc:= FO_COPY; <br>pFrom:= @frombuf; <br>pTo:=@tobuf; <br>fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; <br>fAnyOperationsAborted:= False; <br>hNameMappings:= Nil; <br>lpszProgressTitle:= Nil; <br>end; <br>ShFileOperation( OpStruc ); <br>end;
 
shfileoperation
 
---- 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><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><br>---- 2、删除目录 <br><br>---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。 <br><br>---- 2.1删除目录的递归辅助函数:DoRemoveDir <br><br>function DoRemoveDir(sDirName:String):Boolean;<br>var<br>&nbsp; &nbsp;hFindFile:Cardinal;<br>&nbsp; &nbsp;tfile:String;<br>&nbsp; &nbsp;sCurDir:String;<br>&nbsp; &nbsp;bEmptyDir:Boolean;<br>&nbsp; &nbsp;FindFileData:WIN32_FIND_DATA;<br>begin<br>&nbsp; &nbsp;//如果删除的是空目录,则置bEmptyDir为True<br>&nbsp; &nbsp;//初始时,bEmptyDir为True<br>&nbsp; &nbsp;bEmptyDir:=True;<br>&nbsp; &nbsp;//先保存当前目录<br>&nbsp; &nbsp;sCurDir:=GetCurrentDir;<br>&nbsp; &nbsp;SetLength(sCurDir,Length(sCurDir));<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; 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; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bEmptyDir:=bEmptyDir and True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //不是空目录,置bEmptyDir为False<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bEmptyDir:=False;<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;if sDirName[Length(sDirName)]&lt; &gt;'/' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoRemoveDir(sDirName+'/'+tfile)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoRemoveDir(sDirName+tfile);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not RemoveDirectory(PChar(tfile)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=true;<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;if not DeleteFile(PChar(tfile)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=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;if bEmptyDir then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //返回上一级目录<br>&nbsp; &nbsp; &nbsp; &nbsp; ChDir('..');<br>&nbsp; &nbsp; &nbsp; &nbsp; //删除空目录<br>&nbsp; &nbsp; &nbsp; &nbsp; RemoveDirectory(PChar(sDirName));<br>&nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;//回到原来的目录下<br>&nbsp; &nbsp;ChDir(sCurDir);<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>---- 2.2删除目录的函数:DeleteDir <br><br>function DeleteDir(sDirName: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:=DoRemoveDir(sDirName) and RemoveDir(sDirName);<br>end;<br><br>---- 3、移动目录 <br><br>---- 有了拷贝目录和删除目录的函数,移动目录就变得很简单,只需顺序调用前两个函数即可: <br><br>function MoveDir(sDirName:String;<br>sToDirName:string):Boolean;<br>begin<br>&nbsp; &nbsp; &nbsp;if CopyDir(sDirName,sToDirName) then<br>&nbsp; &nbsp; &nbsp; &nbsp; if RemoveDir(sDirName) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=True<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>end;<br><br>///////////////////////////////////////////////<br>procedure TForm1.Button2Click(Sender: TObject);<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, 'd:/brief/*.*' );<br>&nbsp; StrPCopy( tobuf, 'd:/temp/brief' );<br>&nbsp; With OpStruc DO 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><br>&nbsp; end;<br>&nbsp; ShFileOperation( OpStruc );<br>end;<br>
 
多人接受答案了。
 
后退
顶部