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

x
?
Solved

Detect if Folder Exist's on form close

Posted on 2002-05-13
18
Medium Priority
?
150 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
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 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 450 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

916 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