Solved

Detect if Folder Exist's on form close

Posted on 2002-05-13
18
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

617 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