Solved

delete file

Posted on 2001-09-02
10
170 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

948 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

22 Experts available now in Live!

Get 1:1 Help Now