Search code examples
nsis

NSIS to check installation location is empty or not


How to add functionality about to display override dialog for the installation location.

If user already installed the software, and he is trying to reinstall the software in the same location then i wanted to show information whether do you need to overwrite or not?

I have used below function but it is invoking before opening the location page.

Function .onVerifyInstDirIfFileExists "$INSTDIR\temp.xls" PathGood
PathGood:
    MessageBox MB_OKCANCEL "Do you want to overwrite the location with new installer ?" IDOK lbl_ok IDCANCEL lbl_cancel
    lbl_ok:

    lbl_cancel:
    Quit
FunctionEnd 

Solution

  • .onVerifyInstDir is used to disable the Next button, it should not display UI:

    This code will be called every time the user changes the install directory, so it shouldn't do anything crazy with MessageBox or the like. If this function calls Abort, the installation path in $INSTDIR is deemed invalid.

    If you want to display a message you must use the page leave callback instead:

    !include LogicLib.nsh
    
    Function MyDirLeave
    ${If} ${FileExists} "$INSTDIR\temp.xls"
        MessageBox MB_OKCANCEL "Do you want to overwrite the location with new installer?" IDOK +2
        Abort ; Stay on the current page
    ${EndIf}
    FunctionEnd
    
    Page Directory "" "" MyDirLeave
    Page InstFiles