Executing Batch File in NSIS installer
Asked Answered
P

2

19

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.

Parasynapsis answered 16/7, 2010 at 13:12 Comment(1)
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.Rollins
T
8

Exec[Wait] needs proper quoting:

ExpandEnvStrings $0 %COMSPEC%
ExecWait '"$0" /C "c:\path\to\batch.cmd" "quoted param" normalparam "c:\last param"'
Teacake answered 16/7, 2010 at 16:59 Comment(1)
ignore all of this, see question for what I found.Parasynapsis
B
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

Brumbaugh answered 16/7, 2010 at 13:19 Comment(8)
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%.Ducharme
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.Parasynapsis
To display the output, just a straight Exec will display the Cmd Window: Exec '"$0" /C "C:\Path-to-batch\commands.bat"'Brumbaugh
@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 ExpandEnvStringsParasynapsis
Don't know for sure (Have not used that way before), but direct path to the exec definitely works.Brumbaugh
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.Parasynapsis
@Nedloh, thanks just missed the Update above before my response.Brumbaugh
This did finally work! However, only with "SetOutPath". For what do I need this? What does ExpandEnvStrings do?Dorsiventral

© 2022 - 2024 — McMap. All rights reserved.