怎样删除一个非空目录?(200分)

  • 主题发起人 meckyhan
  • 开始时间
先删除目录中的文件呀<br><br>或者 用Winexec执行Deltree.exe
 
删除目录的递归辅助函数:DoRemoveDir <br>function DoRemoveDir(sDirName:String):Boolean; <br>var <br>hFindFile:Cardinal; <br>tfile:String; <br>sCurDir:String; <br>bEmptyDir:Boolean; <br>FindFileData:WIN32_FIND_DATA; <br>begin <br>//如果删除的是空目录,则置bEmptyDir为True <br>//初始时,bEmptyDir为True <br>bEmptyDir:=True; <br>//先保存当前目录 <br>sCurDir:=GetCurrentDir; <br>SetLength(sCurDir,Length(sCurDir)); <br>ChDir(sDirName); <br>hFindFile:=FindFirstFile('*.*',FindFileData); <br>if hFindFile&lt; &gt;INVALID_HANDLE_VALUE then <br>begin <br>repeat <br>tfile:=FindFileData.cFileName; <br>if (tfile='.') or (tfile='..') then <br>begin <br>bEmptyDir:=bEmptyDir and True; <br>Continue; <br>end; <br>//不是空目录,置bEmptyDir为False <br>bEmptyDir:=False; <br>if FindFileData.dwFileAttributes= <br>FILE_ATTRIBUTE_DIRECTORY then <br>begin <br>if sDirName[Length(sDirName)]&lt; &gt;'/' then <br>DoRemoveDir(sDirName+'/'+tfile) <br>else <br>DoRemoveDir(sDirName+tfile); <br>if not RemoveDirectory(PChar(tfile)) then <br>result:=false <br>else <br>result:=true; <br>end <br>else <br>begin <br>if not DeleteFile(PChar(tfile)) then <br>result:=false <br>else <br>result:=true; <br>end; <br>until FindNextFile(hFindFile,FindFileData)=false; <br>FindClose(hFindFile); <br>end <br>else <br>begin <br>ChDir(sCurDir); <br>result:=false; <br>exit; <br>end; <br>//如果是空目录,则删除该空目录 <br>if bEmptyDir then <br>begin <br>//返回上一级目录 <br>ChDir('..'); <br>//删除空目录 <br>RemoveDirectory(PChar(sDirName)); <br>end; <br><br>//回到原来的目录下 <br>ChDir(sCurDir); <br>result:=true; <br>end; <br>删除目录的函数:DeleteDir <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;
 
调用 deltree, 最简单了,呵呵
 
我有 Turboc 编的删除目录程序
 
利用tshfjileopstruct来进行deltree;<br>procedure deltree(namelist:string);<br>var<br>&nbsp; &nbsp;lpFileOp: TSHFileOpStruct;<br>begin<br>&nbsp; &nbsp;with lpFileOp do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;Wnd := Self.Handle;<br>&nbsp; &nbsp; &nbsp;wFunc := FO_DELETE;<br>&nbsp; &nbsp; &nbsp;pFrom := pchar(NameList + #0);//此为要删除的文件或目录,支持*、?<br>&nbsp; &nbsp; &nbsp;pTo := nil;<br>&nbsp; &nbsp; &nbsp;fFlags := FOF_ALLOWUNDO;<br>&nbsp; &nbsp; &nbsp;hNameMappings := nil;<br>&nbsp; &nbsp; &nbsp;lpszProgressTitle := nil;<br>&nbsp; &nbsp; &nbsp;fAnyOperationsAborted := True;<br>&nbsp; &nbsp;end;<br><br>if SHFileOperation(lpFileOp) &lt;&gt; 0 then<br>&nbsp; &nbsp;ShowMessage('删除失败,请查实。');<br>end;<br>
 
[New - Windows 95]<br><br>Deletes a subfolder from the given folder.<br><br>Syntax<br><br>HRESULT DeleteFolder(ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulUIParam, LPMAPIPROGRESS lpProgress, ULONG ulFlags)<br><br>没试过
 
SHFileOperation
 
要简单就用deltree /y path
 
还是markboy的方法比较妥当!<br>因为调用DelTree不但控制不了它的进度,而且还弹出一个Dos窗口,挺难看的!
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, <br>&nbsp; Controls, Forms, Dialogs, &nbsp;StdCtrls, Buttons,<br>&nbsp; ShellAPI;//要引用ShellApI<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; BitBtn1: TBitBtn;<br>&nbsp; &nbsp; procedure BitBtn1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>var<br>&nbsp; T:TSHfileOpStruct;//要引用ShellApI<br><br>&nbsp; P:String;<br>begin<br>&nbsp; P:='C:/My Documents/test/abc';//要写完整路径。<br>&nbsp; with T do<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;Wnd := 0;<br>&nbsp; &nbsp; &nbsp;wFunc := FO_DELETE;<br>&nbsp; &nbsp; &nbsp;pFrom := pchar(p);<br>&nbsp; &nbsp; &nbsp;pTo := nil;<br>&nbsp; &nbsp; &nbsp;fFlags := FOF_ALLOWUNDO;<br>&nbsp; end;<br>&nbsp; SHFileOperation(T);<br>end;<br><br>end.<br><br>不仅可以删除非空目录,还能在回收站恢复。<br>
 
多人接受答案了。
 
不能删呀,我在win2000server下 c:/send
 
细看markboy的代码,感觉很差劲,功能是实现了,但是代码却太不精炼。比如说:<br>if not RemoveDirectory(PChar(tfile)) then <br>result:=false <br>else <br>result:=true; 可以写成 Result:= RemoveDirectory(PChar(tfile)); 就行了,这样的代码<br>后面还有一句。<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>function DeleteDir(sDirName:String):Boolean; <br>begin <br>&nbsp; if sDirName &lt;&gt; '' then <br>&nbsp; &nbsp; &nbsp;Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName); <br>end; <br>另外,为什么还要判断是不是为空目录?说明作者没有细入到递归内部去,或者说递归没有<br>学好!<br>下面是我写的一个递归删除算法!也可能有很多不精炼的地方,希望给我指正!<br>procedure DelTree(DirName:String);<br>var<br>&nbsp; hFindFile:Cardinal;<br>&nbsp; FileName: String;<br>&nbsp; FindFileData:WIN32_FIND_DATA;<br>begin<br>&nbsp; if DirName[Length(DirName)]&lt;&gt;'/' then<br>&nbsp; &nbsp; DirName:= DirName + '/';<br>&nbsp; hFindFile:= FindFirstFile(PChar(DirName + '*.*'), FindFileData);<br>&nbsp; if hFindFile &lt;&gt; INVALID_HANDLE_VALUE then<br>&nbsp; begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; FileName:= FindFileData.cFileName;<br>&nbsp; &nbsp; &nbsp; if (FileName &lt;&gt; '.') and (FileName &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (FindFileData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DelTree(DirName + FileName)<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DeleteFile(PChar(DirName + FileName));<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; until FindNextFile(hFindFile, FindFileData) = false;<br>&nbsp; &nbsp; Windows.FindClose(hFindFile);<br>&nbsp; &nbsp; RmDir(DirName);<br>&nbsp; end;<br>end;
 
顶部