Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

want to delete all files and folders from a temp folder. 2

Status
Not open for further replies.

DelphiAaron

Programmer
Jul 4, 2002
826
0
16
AU
doen anyone have a snippet of code to delete all files and folders from a temp folder, including folders that have files in them.

thanx

Aaron
 
Isn't it always fortunate when you run into a problem that someone else has right in front of them at the moment as well?

I need to clean it up a little, but I double-checked it and it *should* work.
Code:
procedure m_deltree(filepath: TFileName);
    var
      tfileinfo: TSearchRec;
      r: TFileName;
      retcode: longint;
      atribute: word;
    begin
      r := filepath + '\*.*';
      writeln(r);
      retcode := FindFirst(r, faAnyFile - faVolumeID, tfileinfo);
      while retcode = 0 do
        begin
          if (tfileinfo.attr and faDirectory = faDirectory) then
            begin
            if (tfileinfo.name <> '.') and (tfileinfo.name <> '..') then
                m_deltree(filepath + '\' + tfileinfo.name)
            end
          else
            begin
              atribute := tfileinfo.attr;
              atribute := atribute and not faReadOnly;
              atribute := atribute and not faArchive;
              atribute := atribute and not faSysFile;
              atribute := atribute and not faHidden;
              FileSetAttr(filepath + '\' + tfileinfo.name, atribute);
              if DeleteFile(filepath + '\' + tfileinfo.name) = false then
                writeln('Error deleting ', filepath + '\' + tfileinfo.name);
            end;
          retcode := FindNext(tfileinfo);
        end;
      FindClose(tfileinfo);
      atribute := FileGetAttr(filepath);
      atribute := atribute and not faReadOnly;
      atribute := atribute and not faArchive;
      atribute := atribute and not faSysFile;
      atribute := atribute and not faHidden;
      FileSetAttr(filepath, atribute);
      if RemoveDir(filepath) = false then
         writeln('Error removing ', filepath);
    end;
 
Using FindFirst & FindNext as Glen9999 has shown is the method I prefer.

There are lots of ways to do this...

Here are some others:
Code:
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;

Using the Jedi VCL library (even if you don't use this, I suggest taking a look. It is a library of over over 600 Delphi components). Can be downloaded from:
Code:
uses JCLFileUtils;

DeleteDirectory (Path: string, MoveIntoRecycleBin: boolean);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top