Solved

delete file

Posted on 2001-09-02
10
169 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
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

 
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

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

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

16 Experts available now in Live!

Get 1:1 Help Now