• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Clear text file content Or Delete a text file?

I'm thinking of writing a function that can delete the contents of a text file. Something like clear the contents.
Or is there a way to delete a text file?
0
cybermilky
Asked:
cybermilky
1 Solution
 
esoftbgCommented:
 DeleteFile(FileName);
0
 
esoftbgCommented:
Unit:                     SysUtils

Delphi syntax:        function DeleteFile(const FileName: string): Boolean;
0
 
tobjectpascalCommented:
Function ClearContents(FileN: String): Boolean;
Var
 T: TextFile;
Begin
Result:=False;
 IF FileExists(FileN) Then
  Begin
   System.Assign(T,FileN);
   System.Rewrite(T);
   CloseFile(T);
   Result:=True;
  End;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
 ClearContents('c:\somefile.txt');
end;

Will empty the contents of a file but still leave the file on the hard disk...

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
esoftbgCommented:
Hi cybermilky,
Do you need two equal questions ? If you don't, please delete the previous one until it is empty. I think it is possible.
0
 
Ivanov_GCommented:

   yep, Rewrite(F) should do the work. It the file does not exists - Rewrite create it. If it exists - clear the contents...
0
 
esoftbgCommented:
function  Clear_TextFile_Content(SFileName: string): Bool;
var
  SL:     TStringList;
begin
  SL := TStringList.Create;
  try
    SL.SaveToFile(SFileName);
  finally
    SL.Free;
  end;
end;

procedure TForm1.btn_Empty_TextFileClick(Sender: TObject);
begin
  Clear_TextFile_Content('C:\TF.txt');
end;
0
 
cybermilkyAuthor Commented:
Hi esoftbg,

I get a warning message as below, what's wrong?
[Warning] Unit1.pas(36): Return value of function 'Clear_TextFile_Content' might be undefined
0
 
esoftbgCommented:
Hi cybermilky, excuse me about the omission....

function  Clear_TextFile_Content(SFileName: string): Bool;
var
  B:       Boolean;
  SL:     TStringList;
begin
  B := False;
  SL := TStringList.Create;
  try
    SL.SaveToFile(SFileName);
    B := True;
  finally
    Result := B;
    SL.Free;
  end;
end;
0
 
esoftbgCommented:
the best is:

function  Clear_TextFile_Content(SFileName: string): Bool;
var
  B:       Boolean;
  SL:     TStringList;
begin
  B := False;
  try
    SL := TStringList.Create;
    try
      SL.SaveToFile(SFileName);
      B := True;
    finally
      SL.Free;
    end;
  finally
    Result := B;
  end;
end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now