function DeleteDirectory(NowPath:String):Boolean;  //删除整个目录
var
  search:TSearchRec;
  ret:integer;
  key:string;
begin
  if NowPath[Length(NowPath)]<>'\' then
  NowPath:=NowPath+'\';
  key:=Nowpath+'*.*';
  ret:=findFirst(key,faanyfile,search);
  while ret=0 do begin
  if ((search.Attr and fadirectory)= faDirectory)
    then begin
      if (Search.Name <>'.') and (Search.name<>'..') then
    DeleteDirectory(NowPath+Search.name);
    end else begin
    if ((search.attr and fadirectory)<> fadirectory) then begin
      deletefile(NowPath+search.name);
      end;
    end;
    ret:=FindNext(search);
  end;
  findClose(search);
  removedir(NowPath);
  result:=True;
end;

解决方案 »

  1.   

    谢谢: kevin_gao(困了!累了!睡觉了!) :)
      

  2.   

    还有一个方法,使用shfileoperation函数,可以把需要删除的文件放到回收站中procedure ToRecycle(AHandle: THandle; const ADirName: String);
    var
      SHFileOpStruct: TSHFileOpStruct;
      DirName: PChar;
      BufferSize: Cardinal;
    begin
      BufferSize := Length(ADirName) +1 +1;
      GetMem(DirName, BufferSize);
      try
        FillChar(DirName^, BufferSize, 0);
        StrCopy(DirName, PChar(ADirName));    with SHFileOpStruct do
        begin
          Wnd := AHandle;
          wFunc := FO_DELETE;
          pFrom := DirName;
          pTo := nil;
          fFlags := FOF_ALLOWUNDO;      fAnyOperationsAborted := False;
          hNameMappings := nil;
          lpszProgressTitle := nil;
        end;    if SHFileOperation(SHFileOpStruct) <> 0 then
          RaiseLastWin32Error;
      finally
        FreeMem(DirName, BufferSize);
      end;
    end;
      

  3.   

    TO:cobi(小新国际) 
    你的方法好象没有成功。
      

  4.   

    winexec(pchar('deltree '+name+' /y'),sw_hide);
      

  5.   

    TO:kryso(白河愁) 
    你的方法是作什么的?真的能失掉吗?
      

  6.   

    procedure DeleteDirectory(sPath: string); stdcall;
    var
        srPath: TSearchRec;
        FileAttrs: Integer;
    begin
        FileAttrs := faAnyFile;
        if findFirst(sPath + '\*.*', FileAttrs, srPAth) = 0 then
        begin
            repeat
                if srPath.Attr = faDirectory then
                begin
                    if (srPath.Name <> '.') and (srPath.Name <> '..') then
                        DeleteDirectory(sPath + '\' + srPath.Name);
                end
                else
                begin
                    DeleteFile( sPath +'\'+ srPath.Name );
                    if IOResult <>0 then
                        showmessage('error:'+srPath.Name );
                end;
            until FindNext(srPath) <> 0;
            FindClose(srPath);
        end;
        RmDir(sPath);
    end;我的方法也可以.
    大家交流交流。
    hehe.
      

  7.   

    procedure tform1.button1click(sender:tobject);
     var
      dirinfo:tsearchrec;
      r:integer;
    begin
     r:=findfirst('c:\download\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 direcotry:c:\test');
    end;
      

  8.   

    procedure ToRecycle(AHandle: THandle; const ADirName: String);
    var
      SHFileOpStruct: TSHFileOpStruct;
      DirName: PChar;
      BufferSize: Cardinal;
    begin
      BufferSize := Length(ADirName) +1 +1;
      GetMem(DirName, BufferSize);
      try
        FillChar(DirName^, BufferSize, 0);
        StrCopy(DirName, PChar(ADirName));    with SHFileOpStruct do
        begin
          Wnd := AHandle;
          wFunc := FO_DELETE;
          pFrom := DirName;
          pTo := nil;
          fFlags := FOF_ALLOWUNDO;      fAnyOperationsAborted := False;
          hNameMappings := nil;
          lpszProgressTitle := nil;
        end;    if SHFileOperation(SHFileOpStruct) <> 0 then
          RaiseLastWin32Error;
      finally
        FreeMem(DirName, BufferSize);
      end;
    end;
      

  9.   

    To:Tense
    我的代码配合一个opendialog获取目录名称之后运行正常,怎么会不行呢?
      

  10.   

    不要把简单问题搞复杂了,
    在程序里面写一个.BAT文件,内容如下:
    del XXX/*.*然后用Winexec执行这个.bat
      

  11.   

    正统的方法都太长了,旁门左道就是用winexec运行个delete不就行了?
    大致如下winexec(pchar('delete \y '+pathtodel+'*.*'),sw_hide);
      

  12.   

    除了递归删除目录下的文件、使用shfileoperation函数和应用dos命令外,还有没有别的