请问,如何删除FTP上的非空目录?(75分)

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

葡萄

Unregistered / Unconfirmed
GUEST, unregistred user!
我从网上看到一个函数:<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>//如果删除的是空目录,则置bEmptyDir为True<br>//初始时,bEmptyDir为True<br>&nbsp; bEmptyDir:=True;<br>//先保存当前目录<br>&nbsp; sCurDir:=GetCurrentDir;<br>&nbsp; SetLength(sCurDir,Length(sCurDir));<br>&nbsp; ChDir(sDirName);<br>&nbsp; hFindFile:=FindFirstFile('*.*',FindFileData);<br>&nbsp; if hFindFile&lt;&gt;INVALID_HANDLE_VALUE then<br>begin<br>&nbsp; repeat<br>&nbsp; tfile:=FindFileData.cFileName;<br>&nbsp; &nbsp; if (tfile='.') or (tfile='..') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; bEmptyDir:=bEmptyDir and True;<br>&nbsp; &nbsp; &nbsp; &nbsp; Continue;<br>&nbsp; &nbsp; &nbsp; end;<br>//不是空目录,置bEmptyDir为False<br>&nbsp; &nbsp; bEmptyDir:=False;<br>&nbsp; &nbsp; if FindFileData.dwFileAttributes=<br>&nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_DIRECTORY then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if sDirName[Length(sDirName)]&lt;&gt;'/' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DoRemoveDir(sDirName+'/'+tfile)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DoRemoveDir(sDirName+tfile);<br>&nbsp; &nbsp; if not RemoveDirectory(PChar(tfile)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=false<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not DeleteFile(PChar(tfile)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; end;<br>until FindNextFile(hFindFile,FindFileData)=false;<br>//FindClose(hFindFile);<br>end<br>else<br>&nbsp; begin<br>&nbsp; &nbsp;ChDir(sCurDir);<br>&nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp;exit;<br>end;<br>//如果是空目录,则删除该空目录<br>if bEmptyDir then<br>&nbsp; begin<br>//返回上一级目录<br>ChDir('..');<br>//删除空目录<br>RemoveDirectory(PChar(sDirName));<br>end;<br><br>//回到原来的目录下<br>ChDir(sCurDir);<br>result:=true;<br>end; <br>//&lt;b&gt;2.2删除目录的函数:DeleteDir &lt;/b&gt;<br>function DeleteDir(sDirName:String):Boolean;<br>begin<br>if Length(sDirName)&lt;=0 then<br>exit;<br>//删除... <br>Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);<br>end;<br><br>这个函数只能删除本地目录,希望高手能指点一下如何把它改造成一个可以删除FTP目录的函数,或者用其他方法删除FTP目录,谢谢了,分不多了,全部奉上
 
这个是vfp调用adi<br>移除 ftp 上指定目录<br>&nbsp;<br>你可以自己改造成delphi代码<br>-=-=-=-=-=-=-=-=-=-=-=-=-=-<br>&nbsp;<br><br>* Removing FTP directory &nbsp;<br>* 移除 ftp 上指定目录<br>* 你必需有这个 FTP 上的用户权利<br>*---------------------------------------------<br>clea<br>#Define INTERNET_INVALID_PORT_NUMBER &nbsp; 0<br>#Define INTERNET_OPEN_TYPE_DIRECT &nbsp; &nbsp; &nbsp;1<br>#Define INTERNET_SERVICE_FTP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br>#Define FTP_TRANSFER_TYPE_ASCII &nbsp; &nbsp; &nbsp; &nbsp;1<br>#Define FTP_TRANSFER_TYPE_BINARY &nbsp; &nbsp; &nbsp; 2<br><br>Public hOpen, hFtpSession<br>Do decl<br><br>* select FTP connection providing you an appropriate access level<br>* in all cases it can not be any "anonymous" access<br>If connect2ftp ("202.105.49.131", "down", "down")<br> lnResult = FtpRemoveDirectory (hFtpSession, "红雨")<br><br> * possible reasons for the directory not being removed:<br> * - your access level is not valid<br> * - the directory does not exist<br> * - the directory is not empty<br> If lnResult = 0<br> ? GetLastError() &nbsp; &nbsp;&amp;&amp; usually the error code is 12003<br> Endif<br><br> = InternetCloseHandle (hFtpSession)<br> = InternetCloseHandle (hOpen)<br>Endif<br><br>Procedure &nbsp;decl<br> Declare INTEGER GetLastError IN kernel32<br> Declare INTEGER InternetOpen IN wininet.dll;<br> STRING &nbsp;sAgent,;<br> INTEGER lAccessType,;<br> STRING &nbsp;sProxyName,;<br> STRING &nbsp;sProxyBypass,;<br> STRING &nbsp;lFlags<br><br> Declare INTEGER InternetCloseHandle IN wininet.dll INTEGER hInet<br><br> Declare INTEGER InternetConnect IN wininet.dll;<br> INTEGER hInternetSession,;<br> STRING &nbsp;sServerName,;<br> INTEGER nServerPort,;<br> STRING &nbsp;sUsername,;<br> STRING &nbsp;sPassword,;<br> INTEGER lService,;<br> INTEGER lFlags,;<br> INTEGER lContext<br><br> Declare INTEGER FtpRemoveDirectory IN wininet.dll;<br> INTEGER hConnect,;<br> STRING &nbsp;lpszDirectory<br> Return<br>Endproc<br><br>Function &nbsp;connect2ftp (strHost, strUser, strPwd)<br> * open access to Inet functions<br> hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)<br><br> If hOpen = 0<br> ? "Unable to get access to WinInet.Dll"<br> Return .F.<br> Endif<br><br> * connect to FTP<br> hFtpSession = InternetConnect (hOpen, strHost, INTERNET_INVALID_PORT_NUMBER,;<br> strUser, strPwd, INTERNET_SERVICE_FTP, 0, 0)<br><br> If hFtpSession = 0<br> * close access to Inet functions and exit<br> = InternetCloseHandle (hOpen)<br> ? "FTP " + strHost + " is not available"<br> Return .F.<br> Else<br> ? "Connected to " + strHost + " as: [" + strUser + ", *****]"<br> Endif<br> Return .T.<br>Endfunc<br><br><br>&nbsp;<br>
 
不好意思,刚学Delphi没多久,所以看不懂
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部