Skip to content
⌘ NSIS Forum Archive

Checking if a file exists

14 posts

starfighter5#

Checking if a file exists

The installer I am creating patches some already installed software, as such I want it to check that a certain file exists before allowing it to continue, I have the following.....

Page custom instcheckleave
Function instcheckleave
IfFileExists $INSTDIR\info.ini PathGood
Abort ; if $INSTDIR is not a valid program, don't let us run the patch
PathGood:
FunctionEnd
This code doesn't seem to let the user go any further, regardless of the file existing or not. Where am I going wrong?
Anders#
Calling abort in the pre callback will skip the page, so I don't see how the user can be stuck there no matter what
starfighter5#
Do you think it may be where I am calling it? Here is the page before......

Page custom page4 page4leave
Function page4
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "page4"
!insertmacro MUI_INSTALLOPTIONS_READ $INSTDIR "page4" "Field 1" "State"
FunctionEnd

Page custom page4leave
Function page4leave
IfFileExists $INSTDIR\info.ini PathGood
MessageBox MB_ICONEXCLAMATION|MB_OK "File does not exist"
Abort ;
PathGood:
FunctionEnd
It just gives me the 'File Does not exist' message, even though it does
starfighter5#
The $INSTDIR should be correct, it works fine for the rest of the installation. Everything looks like it should work correctly... strange
jpderuiter#
To be sure, just call MessageBox MB_OK "$INSTDIR\info.ini", and doublecheck if the file really exists.
MSG#
First you do
Page custom page4 page4leave

And then you do
Page custom page4leave

This is not correct. Page4leave is a leave function, not a page function. You call it first as a leave function, but then tell NSIS to create a second page, with the same function...
starfighter5#
Hmmm, it just displays a message saying $INSTDIR\info.ini

Should it display the contents of info.ini?
MSG#
Originally Posted by starfighter5 View Post
Hmmm, it just displays a message saying $INSTDIR\info.ini

Should it display the contents of info.ini?
No, it should display the contents of the $INSTDIR variable. So it should look like "C:\Program Files\YourApp\info.ini". $INSTDIR is a built-in variable, you cannot disable it (as far as I know). Are you sure you typed it correctly?
starfighter5#
This is what I have after the changes and corrections I made, could the problem be that I am bypassing the normal installation directory and setting $INSTDIR from a custom page?

Page custom page4 page4leave
Function page4
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "page4"
!insertmacro MUI_INSTALLOPTIONS_READ $INSTDIR "page4" "Field 1" "State"
FunctionEnd

Function page4leave
IfFileExists "$INSTDIR\info.ini" PathGood
MessageBox MB_OK "$INSTDIR\info.ini"
Abort ; if $INSTDIR is not a valid program, don't let us run the patch
PathGood:
FunctionEnd
starfighter5#
Im still getting nowhere with this issue, do you think it could be that fact that I am setting INSTDIR from a custom page?
starfighter5#
I have just put the following into the function

MessageBox MB_OK "$INSTDIR"

And it displays nothing, but if I put the same into the function below then it displays the installation directory.
starfighter5#
Managed to resolve it, I changed where it reads $INSTDIR to the leave function and things have started working....

Page custom page4 page4leave
Function page4
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "page4"
FunctionEnd

Page custom page4leave
!insertmacro MUI_INSTALLOPTIONS_READ $INSTDIR "page4" "Field 1" "State"
Function page4leave
IfFileExists $INSTDIR\info.ini PathGood
MessageBox MB_ICONEXCLAMATION|MB_OK "File does not exist"
Abort
PathGood:
FunctionEnd