Solved

Detect if Folder Exist's on form close

Posted on 2002-05-13
18
142 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

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

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

18 Experts available now in Live!

Get 1:1 Help Now