一个有关文件的问题?急,请高手赐教!!(100分)

  • 主题发起人 主题发起人 sdcit
  • 开始时间 开始时间
S

sdcit

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样去删除一个目录(要删除这个目录及目录下所有子目录和文件)?
 
写一段递归过程不就OK了嘛!
 
http://www.delphibbs.com/keylife/iblog_show.asp?xid=985
 
function DeleteTree(sPath:string):Boolean;<br>var<br>&nbsp; SearchRec: TSearchRec;<br>begin<br>&nbsp; FindFirst(sPath+'/*.*',faAnyFile,SearchRec);<br>&nbsp; repeat<br>&nbsp; &nbsp; if(SearchRec.Attr and faDirectory &gt; 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if(SearchRec.Name[1]&lt;&gt;'.') then<br>&nbsp; &nbsp; &nbsp; &nbsp; if not DeleteTree(sPath+'/'+SearchRec.Name) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; end else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//如果是文件直接删除<br>&nbsp; &nbsp; &nbsp; if not DeleteFile(sPath+'/'+SearchRec.Name) then<br>&nbsp; &nbsp; &nbsp; &nbsp; break ;<br>&nbsp; until (FindNext(SearchRec)&lt;&gt;0); &nbsp; //继续查找,直到最后<br>&nbsp; FindClose(SearchRec);<br>&nbsp; Result:=ReMoveDir(sPath);<br>end;<br><br>function DelTree(HW:THandle; sPath:string):boolean;<br>var<br>&nbsp; lpFileOp: TSHFileOpStruct;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; with lpFileOp do<br>&nbsp; begin<br>&nbsp; &nbsp; Wnd := HW;<br>&nbsp; &nbsp; wFunc := FO_DELETE;<br>&nbsp; &nbsp; pFrom := pchar(sPath+#0); &nbsp; &nbsp; &nbsp; &nbsp; //此为要删除的文件或目录,支持*、?<br>&nbsp; &nbsp; pTo := nil;<br>&nbsp; &nbsp; fFlags := FOF_ALLOWUNDO;<br>&nbsp; &nbsp; hNameMappings := nil;<br>&nbsp; &nbsp; lpszProgressTitle := nil;<br>&nbsp; &nbsp; fAnyOperationsAborted := True;<br>&nbsp; end;<br>&nbsp; if SHFileOperation(lpFileOp)=0 then<br>&nbsp; &nbsp; Result:=True;<br>end;<br>
 
后退
顶部