Re: Delete a list of files

[ Delphi Forum -- by DelphiLand ]

Posted by webmaster Guido on November 16, 2003 at 17:05:34:

In Reply to: Delete a list of files posted by Johan p14205 on November 15, 2003 at 19:17:07:

: I know how to delete a file, but now I want to delete several files at once (a list). Is there a Delphi command for this where I can give the files in a list?

Let's suppose that the filenames are in a listbox called ListBox1, for example like this:


With a click on button btnDelete we want to delete the files, and also remove them from the listbox to give a visual indication to the user. If a file is not found, we don't remove it from the listbox, but we show an error message.
Note that in the code below, we to work backwards, i.e. we start from the end of the listbox.

procedure TformMain.btnDeleteClick(Sender: TObject);
  i: integer;
  FileName: string;
  for i := ListBox1.Items.Count - 1 downto 0 do begin
    FileName := ListBox1.Items[i];    // get filename from listbox
    if FileExists(FileName) then begin
      DeleteFile(FileName);           // delete file from disk
      ListBox1.Items.Delete(i);       // delete item from listbox
      ShowMessage(FileName + ' not found');

Related Articles and Replies:

[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]