Solved

Clear text file content Or Delete a text file?

Posted on 2004-04-27
9
248 Views
Last Modified: 2010-04-05
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
Comment
Question by:cybermilky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 10935481
 DeleteFile(FileName);
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10935485
Unit:                     SysUtils

Delphi syntax:        function DeleteFile(const FileName: string): Boolean;
0
 
LVL 4

Accepted Solution

by:
tobjectpascal earned 30 total points
ID: 10935520
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:esoftbg
ID: 10935670
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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10935949

   yep, Rewrite(F) should do the work. It the file does not exists - Rewrite create it. If it exists - clear the contents...
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10943530
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
 
LVL 1

Author Comment

by:cybermilky
ID: 10946658
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 10954172
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 10973478
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question