Archive: Validate DirRequest control Path.


Validate DirRequest control Path.
  Hi,


How can I validate the path entered in the DirRequest control.
?
I am using this control on the .ini page.

Right now even if I enters some random characters inside the DirRequest control manually instead of browsing the path. It allows me to go on to the next page.

How can I restrict the user to go on next page until and unless he enters the valid path.

means (e.g. the path should be D:\New\One something like this.)


Use the page's leave function to read the entered value back from the ini file and check if it is a valid path. If it is invalid, call the 'abort' command to stay in the current page (ie stop leaving the page).

How to check a path for validity is is a bit tricky. For existing paths you can use IfFilExists "yourpath\*.*". For a new folder you could perhaps let it try to create the folder and see if it throws an error...


Hi,

Thanks for the reply.

How to catch the errors while creating the directory?

I am using the follwing code......

IfFileExists "$DirectoryPath" 0 CheckForDirectory
strcpy $DIREXISTS "already exists"
CheckForDirectory:
Createdirectory "$DirectoryPath"


ClearErrors

Createdirectory "$DirectoryPath"
>${If} ${Errors}
;
Path is invalid, or user doesnt have access rights to create this directory, or etc etc...
${Else}
;Path is valid
>${EndIf}
Note that this is using LogicLib (link), which I recommend you use. If you don't want to, use the IfErrors command.

See http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.3.3
and http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.4.9


Edit: Note that it's a good idea to remove the directory immediately after this test. The user might later go back to this page and change the path, and you don't want to leave behind empty directories in that case.

As you're using a .INI file, I have to presume you're using InstallOptions(2/Ex).

I would highly recommend changing to nsDialogs if you can as this makes it a lot more flexible.

However...

For InstallOptions, give the directory control a NOTIFY flag. Then when the page 'exits', check whether the exit call came from the directory control, check the path, and call Abort (to prevent the page from actually exiting).
If the path is invalid, disable the Next button (plenty of threads on that topic)

For the user entering text..
As you're using InstallOptions(2), you're stuck to checking the value of the directory control when the user clicks Next - then Abort it if the path is invalid, probably pop up a message that the path they entered manually is invalid.

If you were using InstallOptionsEx, then you could verify the text as the user changes it - just set up a 'ONTEXTCHANGE' flag in the NOTIFY settings for the text field (InstallOptionsEx does not have a single directory, instead relying on a combination of a text control and a button control).
If the path is invalid, disable the Next button.

Again, I recommend looking into nsDialogs if you can.. otherwise, give InstallOptionsEx a shot - it's very similar to InstallOptions, almost being a drop-in replacement (you do have to tweak things!), but gives you a lot more control.


Hi, MSG

I tried according to your code given but I used IfErrors.

My code is as follows...
In the $FilePath variable the path I am giving is "AAAsssss"

Createdirectory "$FilePath"
IfErrors 0 ValidPath
MessageBox MB_OK|MB_ICONEXCLAMATION "Invalid path."
ValidPath:

SO the error flag should set the value right?
But It's not happening. Am I missing something?


Probably CreateDirectory created it as $INSTDIR\AAAssssss or $EXEDIR\AAAssssss or $TEMP\AAAsssss or something like that. You'll have to figure out what exactly it created.

You could StrCpy $0 INSTDIR, StrCpy $INSTDIR "" first and afterwards StrCpy $INSTDIR $0... Though that doesn't win a beauty contest.