Archive: Problem with detecting invalid directory


Problem with detecting invalid directory
Using version 2.0.b3 of NSIS and the MUI, how do I validate the installed directory and why is GetInstDirError an invalid command?


In what way do you want to validate the installation directory?

Make sure you use GetInstDirError inside a Section or Function.

-Stu


GetInstDirError was added in NSIS 2 RC1. It was not present in 2.0b3. To validate the installed directory, use the .onVerifyInstDir callback function.


Hi,

Basically If a user enters an invalid directory, I want to display a message box and return back to the MUI_PAGECOMMAND_DIRECTORY page. So far,the MUI_PAGE_CUSTOMFUNCTION_LEAVE ignores the validation funtion.

...
!define MUI_DIRECTORYPAGE
...

!insertmacro MUI_PAGECOMMAND_LICENSE
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "validateDirectory"
!insertmacro MUI_PAGECOMMAND_DIRECTORY
!insertmacro MUI_PAGECOMMAND_INSTFILES
Page custom ValidateAdminPassword
!insertmacro MUI_PAGECOMMAND_FINISH

...

Function validateDirectory
;display message box if install directory can not be ;created
MessageBox MB_OK|MB_ICONEXCLAMATION \
"Invalid directory try again." IDOK
Abort
End:
FunctionEnd

How do I achieve this using version 2.0.b3 ?

Thanks.


Probably, but you really should upgrade. You might get code that works on the latest version of NSIS, but not on the older one.

However, try this in your leave function:

ClearErrors
SetOutPath $INSTDIR
IfErrors 0 +3
MessageBox ...
Abort

It might not work though. Even in the latest docs it does not say under SetOutPath that the error flag is set if SetOutPath fails...

Edit: A better way would be to use IfFileExists "$INSTDIR\*.*" +3 after SetOutPath.

-Stu

Ok, That is better. It can now detect invalid directory using IfFileExists "$INSTDIR\*.*" . But the back button on the MUI_PAGECOMMAND_INSTFILES page is disabled and there is no way of going back. What do i need to do to enable it?
Thanks


Why would you want to enable the Back button on the InstFiles page?

-Stu


Hello, I want the back button enabled because the validation check actually takes place on the InstFile page, not on the directory page.


You should use the code in the .onVerifyInstDir function, which is used when the user loses focus on the directory field (ie when they click next), and while browsing for a folder. Calling Abort in .onVerifyInstDir will stop the user selecting that path, and thus from continuing off the Directory page in the first place!

-Stu


Hi,

Function .onVerifyInstDir
SetOutPath $INSTDIR
IfFileExists "$INSTDIR\*.*" +2
Abort
End:
FunctionEnd

The downside of the above is that it creats a new folder every time the .onVerifyInstDir function is called. So I ends up with lots of empty folder when editing the install directory.

Removing the set 'SetOutPath $INSTDIR' stops me from specifying a directory that doesn't yet exist. This is because of the 'IfFileExists' test.

What i am after is a way of dectecting if a directory path is valid.


Ah yes, in that case, you'd have to change the code quite a bit...


Function .onVerifyInstDir
IfFileExists "$INSTDIR\*.*" 0 End
Push $R0
ClearErrors
FileOpen $R0 "$INSTDIR\try.tmp" w
FileWrite $R0 ""
FileClose $R0
Pop $R0
IfErrors +3
Delete "$INSTDIR\try.tmp"
Goto End
MessageBox MB_OK|MB_ICONEXCLAMATION "Path is not writable, please enter a new path!"
Abort
End:
FunctionEnd


So basically, this will write a file to the current path if the path exists to check if the path is writable. It won't try and create the path.

You should use the old code from before to check if the final path is writable. You should call that code in your page's Leave function.

For the latest Modern UI, you'd use:
!define MUI_CUSTOMFUNCTION_LEAVE "testInstDir"
!insertmacro MUI_PAGE_INSTFILES

However, you are using an old version therefore I can't help you with setting up a Leave function.

-Stu