Description |
The DeleteFile function deletes a file given by its file name FileName.
The file is looked for in the current directory.
If the file was deleted OK, then True is returned, otherwise False is returned.
This function is easier to use than the equivalent System unit Erase routine.
|
|
Notes |
Warning : the Windows unit also has a DeleteFile function that takes a PChar argument.
To ensure that you use are using the intended one, type SysUtils.DeleteFile.
|
|
Related commands |
|
|
|
|
Example code : Try to delete a file 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 SysUtils, // Unit containing the DeleteFile command 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
fileName : string;
myFile : TextFile;
data : string;
begin // Try to open a text file for writing to
fileName := 'Test.txt';
AssignFile(myFile, fileName);
ReWrite(myFile);
// Write to the file
Write(myFile, 'Hello World');
// Close the file
CloseFile(myFile);
// Reopen the file in read mode
Reset(myFile);
// Display the file contents
while not Eof(myFile) do
begin
ReadLn(myFile, data);
ShowMessage(data);
end;
// Close the file for the last time
CloseFile(myFile);
// Now delete the file
if DeleteFile(fileName)
then ShowMessage(fileName+' deleted OK')
else ShowMessage(fileName+' not deleted');
// Try to delete the file again
if DeleteFile(fileName)
then ShowMessage(fileName+' deleted OK again!')
else ShowMessage(fileName+' not deleted, error = '+
IntToStr(GetLastError));
end; end.
|
Hide full unit code |
Hello World
Test.txt deleted OK
Test.txt not deleted, error = 2
|
|