Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

delete file

Posted on 2001-09-02
10
Medium Priority
?
183 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

782 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