Link to home
Start Free TrialLog in
Avatar of sfern
sfern

asked on

Detect if Folder Exist's on form close

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 :-)
Avatar of wimmeyvaert
wimmeyvaert

Use function DirectoryExists(Name: string): Boolean;

You have to use the unit FileCtrl

If the directory exists, it returns True, else False
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.
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
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
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
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 ...
Avatar of sfern

ASKER

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.

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
Do you want to create this directory also, use :

ForceDirectories('C:\Temp')

If successfull -> True
If NOT successfull -> False

Also uses FileCtrl

Avatar of sfern

ASKER

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

Avatar of sfern

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of wimmeyvaert
wimmeyvaert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Sorry, forgot to mention that the above comment is the explanation of the code snippet I placed here (the last one).
Avatar of sfern

ASKER

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

Avatar of sfern

ASKER

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

Avatar of sfern

ASKER

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 :-)


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.