1. Home
  2. Computing & Technology
  3. Delphi Programming

Delete folders recursively

By , About.com Guide

The following function completely deletes a directory regardless of whether the directory is filled or has subdirectories. No confirmation is requested so be careful. If the operation is successful then True is returned, False otherwise.

Usage

if DelTree('c:\TempDir') then
ShowMessage('Directory deleted!')
else
ShowMessage('Errors occured!') ;

~~~~~~~~~~~~~~~~~~~~~~~~~
uses ShellAPI;
Function DelTree(DirName : string): Boolean;
var
  SHFileOpStruct : TSHFileOpStruct;
  DirBuf : array [0..255] of char;
begin
  try
   Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
   FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
   StrPCopy(DirBuf, DirName) ;
   with SHFileOpStruct do begin
    Wnd := 0;
    pFrom := @DirBuf;
    wFunc := FO_DELETE;
    fFlags := FOF_ALLOWUNDO;
    fFlags := fFlags or FOF_NOCONFIRMATION;
    fFlags := fFlags or FOF_SILENT;
   end;
    Result := (SHFileOperation(SHFileOpStruct) = 0) ;
   except
    Result := False;
  end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» From/to the 8.3 (short) format to/from the long format
« Convert The First Letter In Any Word To A Capital

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 1999 Delphi Tips
  7. Delete folders recursively

©2009 About.com, a part of The New York Times Company.

All rights reserved.