Solved

Clear text file content Or Delete a text file?

Posted on 2004-04-27
9
245 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
9 Comments
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
 DeleteFile(FileName);
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
Unit:                     SysUtils

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

Accepted Solution

by:
tobjectpascal earned 30 total points
Comment Utility
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
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility

   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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now