Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Clear text file content Or Delete a text file?

Posted on 2004-04-27
9
Medium Priority
?
252 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 120 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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