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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete files

Status
Not open for further replies.

cyberant

Programmer
Sep 21, 2003
44
ZA
Hi!

How do I delete all files within a certain directory?
It's easy to delete a single one, but how would I delete them all?

 
Something like this will delete all the files in a directory. You would need to modify the procedure if you want to delete any sub directories (and their files) in the directory as well.
Code:
procedure DeleteAllFiles ( dir: string );
var
 tsr: TSearchRec;
begin
 if DirectoryExists ( dir ) then begin
  dir := IncludeTrailingBackSlash ( dir );
  if FindFirst ( dir + '*.*', 0, tsr ) = 0 then begin
   repeat
    if tsr.name[1] <> '.' then
     DeleteFile ( dir + tsr.name );
   until FindNext ( tsr ) <> 0;
   FindClose ( tsr );
  end;
 end
 else 
  MessageDlg ( dir + ' does not exist', mtError, [mbOK], 0 );
end;

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top