Solved

delete file

Posted on 2001-09-02
10
173 Views
Last Modified: 2010-04-06
I keep deleting one and the same file all over again.How can i programm it so the app. does it for me and after done it terminates automatucally.
Example is :
I keep deleting file:D:\Example\rubbish.dat
How am I tto make an simple app. so it does it for me.???
0
Comment
Question by:Bosanac
  • 6
  • 2
  • 2
10 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6449720
DeleteFile('D:\Example\rubbish.dat');
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6449722
From Delphi help

Deletes a file from disk.

Unit

Sysutils

Category

file management routines

function DeleteFile(const FileName: string): Boolean;

Description

DeleteFile erases the file named by FileName from the disk. If the file cannot be deleted or does not exist, the function returns False.


The Crazy One
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6449732
I put toghether an app that I run when the OS startups up to delete cookies and .tmp and various other files that I don't want hanging around on my system. Perhaps you might want do that as well. The DeleteFile function is real easy to use but make sure of what you are deleting when using it because the function does not use the Recycle Bin.
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 1

Expert Comment

by:Dennis9
ID: 6450724
Hi.
Just do this in the forms oncreate event:

///Example
procedure TForm1.FormCreate(Sender: TObject);
begin
DeleteFile('D:\Example\rubbish.dat');
Application.ProcessMessages;
Application.Terminate;
end;


Dennis
0
 

Author Comment

by:Bosanac
ID: 6450732
Is this OK??

if FileExists('D:\Private') then begin
 DeleteFile('D:\Private');
  Application.terminate;
 end
 else  begin showmessage ('File does not exist !');
 application.processmessages;
 application.terminate;

 end;
end;
0
 
LVL 1

Expert Comment

by:Dennis9
ID: 6450749
This will work, u need to have the extension with it.

Var
str:string;

Begin
str:= 'D:\Example\rubbish.dat';

if FileExists(str) then
begin
DeleteFile(str);
application.processmessages;
Application.terminate;
end
else  
begin
showmessage ('File '+str+' does not exist!');
application.processmessages;
application.terminate;
end;

Dennis
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6450927
Hi Bosanac
Yes your code should work as long as Private is a file and not a folder. Personally this is the way I would do it.

var
     sFileToDel, sMsg: string;

begin
     sFileToDel := 'D:\Private';
     if not DeleteFile(sFileToDel) then begin

          if not FileExists(sFileToDel) then
               sMsg := 'File not found'
          else
               sMsg := 'Could not delete file';

          ShowMessage(sMsg);
     end;

end;    
0
 

Author Comment

by:Bosanac
ID: 6450980
Why is it that my form pops up during runtime (deleting or whatever...)though I declared form1.visible:=false????
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6451162
Well this is the way I do it.

procedure TForm1.FormCreate(Sender: TObject);
begin
     Form1.Visible := False;
     try
          DoYourCode;
     finally
          Form1.Free;
     end;
end;
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 50 total points
ID: 6451177
If the form is not the main form you could do it like this.

procedure TForm1.FormCreate(Sender: TObject);
begin
     Form1.Visible := False;
     try
          DoYourCode;
     finally
          Form1.Free;
              if not Application.Terminated then
               Application.Terminate;
     end;
end;
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 69
how can i search if string exist in array ? 3 69
RESTRequest Parameter 4 53
Installshield for Embarcadero EX 10.1 Berlin 4 72
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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