SmartPascal
Erase
Procedure
Erase a file System unit
 procedure Erase ( var FileHandle : File; ) ;
Description
The Erase procedure attempts to erase a file given by the FileHandle.
 
The file name must have been assigned to the given FileHandle by the AssignFile  routine.
 
If the file does not exist, then the EInOutError exception is raised.
Related commands
DeleteFile Delete a file specified by its file name
Rename Rename a file
RenameFile Rename a file or directory
 
Example code : Create a simple file, then try to delete it twice
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myFile : TextFile;

begin
  // Let us open a text file
  AssignFile(myFile, 'Test.txt');
  ReWrite(myFile);

  // And write a single line to it
  WriteLn(myFile, 'Hello World');

  // Then close it
  CloseFile(myFile);

  // And finally erase it
  Erase(myFile);

  // If we try to raise it again, we'll raise an exception
  try
    Erase(myFile);
  except
    on E : Exception do
      ShowMessage('Cannot erase : '+E.Message);
  end;
end;
 
end.
Hide full unit code
   Cannot erase : File not found