请问各位高手,如果使Delphi实现Deltree的功能啊?(1分)

  • 主题发起人 主题发起人 InvidentXp
  • 开始时间 开始时间
I

InvidentXp

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位高手,如果使Delphi实现Deltree的功能啊?
 
这应该是个算法和文件访问的问题,跟具体语言无关。<br>算法:树的后序遍历,递归。<br>文件访问:读第一个目录项,读下一个目录项,删除文件。
 
可是我是菜鸟啊,能不能给出完整的代码?
 
uses shellapi;<br><br>{$R *.DFM}<br><br>&nbsp;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br><br>Var<br><br>&nbsp; T:TSHFileOpStruct;<br><br>&nbsp; P:String;<br><br>begin<br><br>&nbsp; P:='d:/address';//这里改成你要删除的任意目录名<br><br>&nbsp; With T do<br><br>&nbsp; Begin<br><br>&nbsp; Wnd:=0;<br><br>&nbsp; wFunc:=FO_DELETE;<br><br>&nbsp; pFrom:=Pchar(P);<br><br>&nbsp; pTo:=nil;<br><br>&nbsp; fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息<br><br>&nbsp; hNameMappings:=nil;<br><br>&nbsp; lpszProgressTitle:='正在删除文件夹';<br><br>&nbsp; fAnyOperationsAborted:=False;<br><br>&nbsp; End;<br><br>&nbsp; SHFileOperation(T);<br><br>end; <br>
 
//---------------删除目录树------------------------------------------------<br>FUNCTION FkDeleteTree(vFolder:STRING):Boolean;<br>VAR<br>&nbsp; srList: TSearchRec;<br>&nbsp; DirList: TStringList;<br>&nbsp; oFound,oTemp: Boolean;<br>&nbsp; i: integer;<br>BEGIN<br>&nbsp; oTemp:=True;<br>&nbsp; // 建立一个文件夹列表<br>&nbsp; DirList := TStringList.Create;<br>&nbsp; vFolder:=TRIM(vFolder);<br>&nbsp; IF vFolder[length(vFolder)]&lt;&gt;'/' THEN &nbsp;vFolder:=vFolder + '/';<br>&nbsp; &nbsp;// 生成文件夹列表<br>&nbsp; oFound:=FindFirst(vFolder+'*.*',(faDirectory+faHidden+faSysFile), srList) = 0;<br>&nbsp; WHILE oFound DO<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; IF (DirectoryExists(vFolder+srList.Name) and (srList.Name&lt;&gt;'.') AND (srList.Name&lt;&gt;'..')) &nbsp;THEN<br>&nbsp; &nbsp; &nbsp; DirList.Add(vFolder+srList.Name);<br>&nbsp; &nbsp; oFound :=(FindNext(srList)=0);<br>&nbsp; &nbsp; END;<br>&nbsp; FindClose(srList);<br>&nbsp; //查找当前目录的文件删除<br>&nbsp; oFound:=FindFirst(vFolder+'*.*',(faAnyFile+faHidden+faSysFile+faReadOnly), srList) = 0;<br>&nbsp; WHILE oFound DO<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; FileSetAttr(vFolder+srList.Name,0);<br>&nbsp; &nbsp; oTemp:=DeleteFile(vFolder+srList.Name) and oTemp;<br>&nbsp; &nbsp; oFound:=(FindNext(srList)=0);<br>&nbsp; &nbsp; END;<br>&nbsp; FindClose(srList);<br>&nbsp; //查找列表的子目录<br>&nbsp; FOR i := 0 TO DirList.Count-1 DO &nbsp;FkDeleteTree(DirList);<br>&nbsp; FileSetAttr(vFolder,0);<br>&nbsp; oTemp:=RemoveDir(vFolder) AND oTemp;<br>&nbsp; DirList.Free;<br>&nbsp; Result:=oTemp;<br>END;<br>//--------------------------------------------------------------<br>
 
接受答案了.
 
后退
顶部