1. Technology

Delete folders recursively

By

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

©2014 About.com. All rights reserved.