?
Solved

Clear text file content Or Delete a text file?

Posted on 2004-04-27
9
Medium Priority
?
250 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
Technology Partners: 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

Independent Software Vendors: 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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