Search code examples
batch-filensisbatch-processing

How to create an exe which work for both Program Files and Program Files(x86)?


I am trying to create an exe file using NSIS script, in my script i am copying a batch file and a folder inside the already installed Jasper Server directory(It could be any directory)

This is my NSIS script--

   * Section 
     SetOutPath "C:\PROGRA~2\JASPER~1.0\buildomatic"

      File /r "C:\Users\Desktop\K-installer\backup3101"
       File "C:\Users\Desktop\K-installer\batch\demo.bat"
         ExecWait '"C:\PROGRA~2\JASPER~1.0\buildomatic\demo.bat"'
    SectionEnd  *

that is working fine when jasper server is installed in Program Files(x86). How can i generalize it for both Program Files and Program Files(x86).


Solution

  • This might not be super elegant but you can check if the directory "C:\PROGRA~2\JASPER~1.0\buildomatic" exists for "Program Files(x86)" and check if "C:\PROGRA~1\JASPER~1.0\buildomatic" for "Program Files". Then you can can do individual branching and handle everything according to your needs there.

    Here you find how:

    http://nsis.sourceforge.net/IfFileExists_Changes_Section_Flags

    I hope this has helped you.

    Edit: You might want to try something like this (warning not tested):

    Section /o "Program Files(x86)"   prg2
        SetOutPath "C:\PROGRA~2\JASPER~1.0\buildomatic"
        File /r "C:\Users\Desktop\K-installer\backup3101"
        File "C:\Users\Desktop\K-installer\batch\demo.bat"
        ExecWait '"C:\PROGRA~2\JASPER~1.0\buildomatic\demo.bat"'
    SectionEnd
    
    Section /o "Program Files)"   prg1
        SetOutPath "C:\PROGRA~1\JASPER~1.0\buildomatic"
        File /r "C:\Users\Desktop\K-installer\backup3101"
        File "C:\Users\Desktop\K-installer\batch\demo.bat"
        ExecWait '"C:\PROGRA~1\JASPER~1.0\buildomatic\demo.bat"'
    SectionEnd
    
    Function .onInit
    IfFileExists C:\PROGRA~1\JASPER~1.0\buildomatic Prog1Exists PastProg1Exists
    Prog1Exists:
      ; Use the macro from sections.nsh
      !insertmacro SelectSection ${prg1}
    PastProg1Exists:
    
    IfFileExists C:\PROGRA~2\JASPER~1.0\buildomatic Prog2Exists PastProg2Exists
    Prog2Exists:
      ; Use the macro declared above
      !insertmacro SelectSection ${prg2}
    PastProg2Exists:
    
    FunctionEnd