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

How do I delete all files in a folder? 1

Status
Not open for further replies.

Jayz

ISP
Feb 17, 2002
59
0
0
I would of thought a simple command like the below would work?

DeleteFile('C:\temp\*.*);

 
Nope. Deletefile handles one file at a time.
Try this...
Code:
procedure deleteallfiles(dir : string);
  var sl   : tstringlist;
      sr   : tsearchrec;
      i,mx : integer;
  begin
  sl := tstringlist.create;
  try
    sl.clear;
    if findfirst(dir+'*.*',faanyfile,sr) = 0
    then begin
         repeat
           sl.add(sr.name);
         until findnext(sr) <> 0;
         findclose(sr);
         end;
    if messagedlg('About to delete '
                 +inttostr(sl.count)
                 +' files. Continue?,
                  mtconfirmation,
                  [mbyes,mbno],0) = mryes
    then begin
         mx := sl.count - 1;
         for i := 0 to mx
         do deletefile(sl[i]);
         end;
  finally
    sl.free; 
  end;
  end;
If it's stupid but it works, it isn't stupid
 
I found this code on the net which I slightly modified and it has worked perfectly for me.

found here:

I still don't understand the workings of it and whether this is the right way to go about deleting files in a directory.
Maybe someone with some expertise can explain what's going on.

var

DirInfo: TSearchRec;
r : Integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
r := FindFirst('C:\tmp\*.*', FaAnyfile, DirInfo);
while r = 0 do begin
if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
(DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
if DeleteFile(pChar('C:\tmp\' + DirInfo.Name))
= false then
ShowMessage('Unable to delete : C:\tmp\' +
DirInfo.Name);
r := FindNext(DirInfo);
end;
SysUtils.FindClose(DirInfo);

end;



end.
 
TSearchRec dictates the information that the FindFirst or FindNext functions search for. In your example code, DirInfo is of this type. What the code is doing is this (i'll write it in a kind of pseudo code!):

Find the first file matching the specified criteria (i.e. look in C:\tmp\ for any file) and store the result of this find operation in integer variable r (FindFirst returns 0 if a file was successfully located)
while the result of the find operation is successful (i.e. r = 0) do
if (the file is not a Directory file) and (the file is not a Volume ID file) then
if the delete file operation was unsuccessful on the specified file then
display message &quot;Unable to delete specified file&quot; (DirInfo.Name represents the filename including the extension)
end if
end if
find the next file matching the above criteria and store result in r
end while
release memory allocated by FindFirst (thus terminating the FindFirst/FindNext sequence)

Note: DirInfo.Attr represent the file attributes of the file. See the helpfile under &quot;TSearchRec&quot; for information about testing for certain attributes. Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
I have written a recursive procedure to delete the whole directory inclding the sub folder. Hope it helps.

procedure DeleteOneDirectory;
var
l_attr : word;
l_SearchRec : TSearchRec;
begin
if FindFirst(path+'\*.*',faAnyFile,l_SearchRec) = 0 then
begin
Repeat
if (l_SearchRec.Name <> '.') and (l_SearchRec.Name <> '..') then
begin
if not DeleteFile (PChar(path+'\'+l_SearchRec.Name)) then
begin
l_attr := FileGetAttr(path+'\'+l_SearchRec.Name);
if (l_attr and faReadOnly <> 0) or
(l_attr and faHidden <> 0) or
(l_attr and faSysFile <> 0) then
begin
FileSetAttr(path+'\'+l_SearchRec.Name, faArchive);
if DeleteFile (PChar(path+'\'+l_SearchRec.Name)) then
; {should be able to delete now}
end
else
if (l_attr and faDirectory <> 0) then
DeleteOneDirectory (path+'\'+l_SearchRec.Name, AShowErr)
end
end;
until FindNext(l_SearchRec) <> 0;
FindClose(l_SearchRec);
end;
end;
 
Thankyou all for your posts. Clive that was well explained. I couldn't have done it better myself.

Regards,
Jayz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top