18

I have a batch file that I need to run within my NSIS installer. It must run after all the files have been extracted, (I suppose this is obvious, otherwise the batch file wouldn't exist yet).

I tried to use MUI_PAGE_CUSTOMFUNCTION_PRE with the finish page in order to run it but when it gets to that portion of the script it appears that it skips right over it. Below is how I invoke it.

;;Finish Page
!define MUI_PAGE_CUSTOMFUNCTION_PRE Done
!insertmacro MUI_PAGE_FINISH

Function Done
    ExecWait '"$INSTDIR\BatchFile" "$INSTDIR" "$DATA_FOLDER"'
FunctionEnd

Thanks in advance for your help.

UPDATE

I have now tried using the following:

ExpandEnvStrings $0 %COMSPEC% 
ExecWait '"$0" /C "$INSTDIR\batch.bat" "$INSTDIR" "$DATA_FOLDER"'

This did not work, so I took out the /C to see what the cmd prompt was saying (it is popping up, but closing immediately) and it seems as though it executes cmd.exe but that's it, it doesn't complete the rest of the execute.

UPDATE #2

The core knowledge that led to me getting it to work can be found here:

Windows batch files: .bat vs .cmd?

For whatever reason .bat files do not agree with ExecWait.

In the end:

ExecWait '"$INSTDIR\BatchFile.cmd" "$INSTDIR" "$DATA_FOLDER"'

Worked just fine.

Community
  • 1
  • 1
Nedloh
  • 381
  • 2
  • 3
  • 14
  • I was able to run .bat file, just as you've posted above for cmd files. You can also suppress the command window from being displayed by using nsExec::Exec, rather than ExecWait. – Ben Nov 04 '11 at 19:56

2 Answers2

8

Exec[Wait] needs proper quoting:

ExpandEnvStrings $0 %COMSPEC%
ExecWait '"$0" /C "c:\path\to\batch.cmd" "quoted param" normalparam "c:\last param"'
Anders
  • 97,548
  • 12
  • 110
  • 164
6

I have done this using an exec extension very successfully

This is the syntax:

  SetOutPath $INSTDIR\${APPLICATION_DIR}
    ExpandEnvStrings $0 %COMSPEC%
    nsExec::ExecToStack '"C:\path-tobatch-file\commands.bat"'

Here is a link to the NSIS Wiki http://nsis.sourceforge.net/Docs/nsExec/nsExec.txt

DBQ
  • 189
  • 4
  • 2
    Just to clarify a bit. Batch files need to be run via command processor: `cmd /c batch_file.bat` instead of just `batch_file.bat`. One can get path to `cmd` from %COMSPEC%. – atzz Jul 16 '10 at 13:28
  • That worked great, however, is there a way to set it so that the console will display while the batch file is executing? The batch file does a copy/move of a decently large number of files and I don't want the user to think that it's not doing anything when in reality it is. – Nedloh Jul 16 '10 at 13:49
  • To display the output, just a straight Exec will display the Cmd Window: Exec '"$0" /C "C:\Path-to-batch\commands.bat"' – DBQ Jul 16 '10 at 13:58
  • @DBQ: Hmmm having issues with that, This should work right Exec '"$0" /C "$INSTDIR\batch.bat" "$INSTDIR" "$DATA_FOLDER"' as long as I did the SetOutPath and ExpandEnvStrings – Nedloh Jul 16 '10 at 14:26
  • Don't know for sure (Have not used that way before), but direct path to the exec definitely works. – DBQ Jul 19 '10 at 13:26
  • AS I updated above, NSIS Exec[Wait] call does not like .bat file for some reason. I changed it to a .cmd extension and my original attempt worked just fine. – Nedloh Jul 19 '10 at 15:58
  • @Nedloh, thanks just missed the Update above before my response. – DBQ Jul 20 '10 at 13:32
  • This did finally work! However, only with "SetOutPath". For what do I need this? What does ExpandEnvStrings do? – Natalie May 19 '16 at 08:35