procedure TForm1.Button1Click(Sender: TObject);
var dirinfo:tsearchrec;r:integer;
begin
r:=SysUtils.FindFirst('c:\test\*.*',faanyfile,dirinfo);
while r=0 do
begin
if ((dirinfo.attr and fadirectory<>fadirectory)and (dirinfo.attr and favolumeid<>favolumeid)) then
if deletefile(pchar('c:\test\'+dirinfo.name))=false then
showmessage('unable to delete:c:\test\'+dirinfo.name);
r:=findnext(dirinfo);
end;
sysutils.findclose(dirinfo);
if removedirectory('c:\test')=false then
showmessage('unable to delete:c:\test');
end;

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var dirinfo:tsearchrec;r:integer;
    begin
    r:=SysUtils.FindFirst('c:\test\*.*',faanyfile,dirinfo);
    while r=0 do
    begin
    if ((dirinfo.attr and fadirectory<>fadirectory)and (dirinfo.attr and favolumeid<>favolumeid)) then
    if deletefile(pchar('c:\test\'+dirinfo.name))=false then
    showmessage('unable to delete:c:\test\'+dirinfo.name);
    r:=findnext(dirinfo);
    end;
    sysutils.findclose(dirinfo);
    if removedirectory('c:\test')=false then
    showmessage('unable to delete:c:\test');
    end;
      

  2.   

    删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录则置bEmptyDir为True,这一句已经用深色框表示了。
    删除目录的递归辅助函数:DoRemoveDir
    function DoRemoveDir(sDirName:String):Boolean;varhFindFile:Cardinal;tfile:String;sCurDir:String;bEmptyDir:Boolean;FindFileData:WIN32_FIND_DATA;begin//如果删除的是空目录则置bEmptyDir为True//初始时bEmptyDir为TruebEmptyDir:=True;//先保存当前目录sCurDir:=GetCurrentDir;SetLength(sCurDirLength(sCurDir));ChDir(sDirName);hFindFile:=FindFirstFile('*.*'FindFileData);if hFindFile< >INVALID_HANDLE_VALUE thenbeginrepeattfile:=FindFileData.cFileName;if (tfile='.') or (tfile='..') thenbeginbEmptyDir:=bEmptyDir and True;Continue;end;//不是空目录置bEmptyDir为FalsebEmptyDir:=False;if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY thenbeginif sDirName[Length(sDirName)]< >'\' thenDoRemoveDir(sDirName+'\'+tfile)elseDoRemoveDir(sDirName+tfile);if not RemoveDirectory(PChar(tfile)) thenresult:=falseelseresult:=true;endelsebeginif not DeleteFile(PChar(tfile)) thenresult:=falseelseresult:=true;end;until FindNextFile(hFindFileFindFileData)=false;FindClose(hFindFile);endelsebeginChDir(sCurDir);result:=false;exit;end;//如果是空目录则删除该空目录if bEmptyDir thenbegin//返回上一级目录ChDir('..');//删除空目录RemoveDirectory(PChar(sDirName));end;
    //回到原来的目录下ChDir(sCurDir);result:=true;end;删除目录的函数:DeleteDir
    function DeleteDir(sDirName:String):Boolean;beginif Length(sDirName)< =0 thenexit;//删除...Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);end;
      

  3.   

    呵呵...
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var F:TShFileOpStruct;    //uses shellapi;
        b:integer;
    begin
       f.Wnd:=Form1.Handle;
       f.wFunc:=fo_delete; 
       f.pFrom:=pChar('c:\windows\desktop\test');
       f.fFlags:=FOF_ALLOWUNDO or  FOF_NOCONFIRMATION;
       b:=ShFileOperation(f);
       if b=0  then exit;
       beep; ShowMessage('文件操作没有实现!');
    end;