Solved

Detect if Folder Exist's on form close

Posted on 2002-05-13
18
144 Views
Last Modified: 2010-04-04
Hi Experts,

Can you please help me in this one?

I need some code which when i try to close the form
or terminate the application, with check if a folder
exists and if it exist's will prompt me if i want to
close the application.

Thanks

SFern :-)
0
Comment
Question by:sfern
  • 9
  • 6
  • 3
18 Comments
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005625
Use function DirectoryExists(Name: string): Boolean;

You have to use the unit FileCtrl

If the directory exists, it returns True, else False
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005635
Here is a code snippet :


procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;  { Application won't quit. }
  if DirectoryExists('C:\Temp') then
  begin
    if MessageDlg('Are you sure you want to quit the application ?', mtInformation, [mbYes, mbNo], 0) = mrYes then
    begin
      CanClose := True;
    end;
  end;
end;

Be sure to have FileCtrl in your uses-list.

Remark : If the directory C:\Temp doesn't exist, then you won't be able to close the application.
0
 
LVL 17

Expert Comment

by:geobul
ID: 7005636
Hi,

uses FileCtrl;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
  if DirectoryExists('c:\temp') then
    if MessageDlg('Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then Action := caFree;
end;

Regards, Geo
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005661
I personally think it's better to do your actions in the OnCloseQuery-Event. This is triggered before the OnClose-Event.

Unless you can convince me of doing this kind of things in the OnClose-Event ;-)

From Borland Delphi Help :

Use OnCloseQuery to specify the conditions under which the form can close. An OnCloseQuery event handler contains a Boolean CanClose variable that determines whether a form is allowed to close. Its default value is True.

Best regards,
The Mayor
0
 
LVL 17

Expert Comment

by:geobul
ID: 7005714
Similar text is written for OnClose (I use D5) and OnClose is more powerful (you can also hide or minimize the form). That's why I prefer that way.

Both ways are possible in this case. The result will be the same.

Regards, Geo
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005723
Maybe the OnCloseQuery is better if you just want to determine if the Form should be closed or not.

Like you say, the OnClose probably is better if you want to do other things (like minimize, hide, ...) with the form.

Never looked at it this way ...
0
 

Author Comment

by:sfern
ID: 7005734
Hi there,

It's working fine, but i've got a little problem,

The problem arrives when the directory does not exists, i cannot close the form, i don't even get the prompt.

The problem here is that is the directory exists you are asked to exit yes or no...and works fine it both cases but if the directory does not exists i don't get the prompt and cannot close the form either.

Please help, i will upgrade my points for the right answer.

Thanks

Sfern.

0
 
LVL 17

Expert Comment

by:geobul
ID: 7005751
1. When the folder doesn't exist - exit without ptompt:

uses FileCtrl;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 Action := caFree;
 if DirectoryExists('c:\temp') then
   if MessageDlg('Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then Action := caNone;
end;

2. When the folder doesn't exist - ask another ptompt:

uses FileCtrl;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 Action := caNone;
 if DirectoryExists('c:\temp') then begin
   if MessageDlg('Directory exists! Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then Action := caFree;
 end else
  if MessageDlg('Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then Action := caFree;
end;

Regards, Geo
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005753
Do you want to create this directory also, use :

ForceDirectories('C:\Temp')

If successfull -> True
If NOT successfull -> False

Also uses FileCtrl

0
 

Author Comment

by:sfern
ID: 7005764
It's basically this:

1. Detect is Folder exists.
2. If it exists: You get a Prompt with 2 Options
   
If Yes is clicked...Do Something...and Close the    application.

If No is Clicked...Do Something...and Close the application.

OTHERWISE:

If the Folder Does not exists...Close the application.


This cenario with appear on the ...CloseQuery... and an exit button.


I'm now upgrading the points for the right answer.

Thanks Again

Sfern

0
 

Author Comment

by:sfern
ID: 7005767
It's basically this:

1. Detect is Folder exists.
2. If it exists: You get a Prompt with 2 Options
   
If Yes is clicked...Do Something...and Close the    application.

If No is Clicked...Do Something...and Close the application.

OTHERWISE:

If the Folder Does not exists...Close the application.


This cenario with appear on the ...CloseQuery... and an exit button.


I'm now upgrading the points for the right answer.

Thanks Again

Sfern

0
 
LVL 6

Accepted Solution

by:
wimmeyvaert earned 150 total points
ID: 7005770
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if DirectoryExists('C:\Temp') then
 begin
   if MessageDlg('Your prompt with 2 options', mtInformation, [mbYes, mbNo], 0)
= mrYes then
   begin
     // Do you actions if yes was pressed
   end
   else
   begin
     // Do you actions if no was pressed
   end;
 end;
end;
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005779
If the directory exists, then a question is asked first.

If the user pressed 'Yes' on this question, then you can code some actions before the form is closed. This is the code in the begin/end of the if-statement

If he/she pressed 'No', you can place you code in the second begin/end. This code is in the begin/end of the else-block of the if-statement.

At last, the form will be closed.
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005785
Sorry, forgot to mention that the above comment is the explanation of the code snippet I placed here (the last one).
0
 

Author Comment

by:sfern
ID: 7005808
It works but what about when the folder does not exists?

Nothing comes out and the application does not close, because there's nothing to say to close the form if none if the cenarios happen

Thanks


Sfern

0
 

Author Comment

by:sfern
ID: 7005819
It works but what about when the folder does not exists?

Nothing comes out and the application does not close, because there's nothing to say to close the form if none if the cenarios happen

Thanks


Sfern

0
 

Author Comment

by:sfern
ID: 7005823
OK, found the way to do it.

I basically continued the statement and it now works.

Thanks all for your help.

wimmeyvaert...here 150 point for you and thanks to all.

SFern

BFN :-)


0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 7005829
I have just tested my last code-snippet, and my application closes when the directory does not exist, without asking anything.

Maybe you still use my first code snippet in your project.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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