- NSIS Discussion
- How can I execute a batch file?
Archive: How can I execute a batch file?
MidnightJava
15th June 2007 06:06 UTC
How can I execute a batch file?
I'm trying to execute a .bat file in a function. I used
Exec $INSTDIR\installRCB.bat
But the file is not executed. I see a command window pop up very briefly, but nothing more. Is Exec supposed to work only with .exe files? Is there a way to execute the .bat file?
demiller9
15th June 2007 06:39 UTC
There are several ways to execute a batch file. Check this forum thread for several ways. It depends whether you want the script to wait, if you want the command window to be visible, if you need to capture or redirect the batch output.
Don
MidnightJava
15th June 2007 06:43 UTC
I've seen other examples where batch files are executed with Exec, so I must be doing something wrong. Here's how I invoke the function
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE installServices
!insertmacro MUI_PAGE_FINISH
and here's the function:
Function installServices
Exec '$INSTDIR\installRCB.bat'
FunctionEnd
The file installRCB.bat is verified to be in $INSTDIR, and if I execute it manually it runs properly. But when executed from the installer, nothing happens. I've tried ExecWait also, and every permutation of single and double quotes, to no avail.
MidnightJava
15th June 2007 07:00 UTC
Looks like I had more of a scripting problem than an NSIS issue. I had relative path references in my batch file, which were not working when the script was executed from the installer. The script was written assuming it would be executed from the directory that equates to $INSTDIR in the installer. But apparently .bat files run from within the installer do not execute from $INSTDIR. Thanks for your help.
Takhir
15th June 2007 07:04 UTC
NSIS Exec can start binary file only, bat files managed by shell. First way is to use ExecShell, but this flashes black console window. So find something sutable for you in the forum 'search' on ComSpec keyword http://forums.winamp.com/search.php?...der=descending
MidnightJava
15th June 2007 13:01 UTC
Actually it's working now. I was trying to say that in my last post, but I wasn't very clear. I was executing a .bat file that had two lines in it. Each line was calling another .bat script, identifying them using relative path references whose base path is the path location where the first .bat file was executing (the directory that the NSIS installer knows as $INSTDIR).
Since the NSIS installer was executing from the location where the setup file was located, which is not $INSTDIR, it was unable to resolve the two relative path references in the .bat file I was calling from the installer. That's why I was seeing the cmd window pop up momentarily. It was executing the .bat file, but it was unable to find any command that was meaningful.
To resolve the problem I eliminated the .bat file that was calling the other two .bat files. Instead I just call each of those .bat files directly from the installer, using two successive Exec commands.
Because of this, and several other examples I saw by searching the forum, it seems clear that Exec does indeed support execution of .bat files.
Afrow UK
15th June 2007 13:02 UTC
I would recommend that you use COMSPEC /C (as suggested) to execute the batch files just in case.
Stu
MidnightJava
15th June 2007 22:01 UTC
OK I see what COMSPEC is about, and I'll use that if you think it's a more universal solution. But what about using nsExec instead of the /C switch with COMSPEC? I imagine COMSPEC /C will still flash the cmd window momentarily, while nsExec won't? And should i still use COMSPEC with nsExec?
But more to the point, how do I get info on how to use nsExec? I went to the NSIS plug-in page and it's not listed. I have the .dll file in my NSIS plug-ins directory, but the only info I can gather is a list of nsExec functions at the top of my compiler output.
Red Wine
15th June 2007 23:32 UTC
Originally posted by MidnightJava
OK I see what COMSPEC is about, and I'll use that if you think it's a more universal solution. But what about using nsExec instead of the /C switch with COMSPEC? I imagine COMSPEC /C will still flash the cmd window momentarily, while nsExec won't? And should i still use COMSPEC with nsExec?
But more to the point, how do I get info on how to use nsExec? I went to the NSIS plug-in page and it's not listed. I have the .dll file in my NSIS plug-ins directory, but the only info I can gather is a list of nsExec functions at the top of my compiler output.
I think you have point here, probably nsExec would do the job as you mentioned.
See under your local nsis installation into folders documents and examples for info about nsExec.
Afrow UK
16th June 2007 12:18 UTC
nsExec with COMSPEC will work fine.
Stu
MidnightJava
17th June 2007 05:15 UTC
Just to close the loop here:
Both Exec and nsExec worked fine and did what I needed. I implemented as follows:
Function installServices
ReadEnvStr $0 COMSPEC
nsExec::Exec '$0 /c "$INSTDIR\RCBClient\bin\InstallRCBClient-NT.bat"'
nsExec::Exec '$0 /c "$INSTDIR\RCBServer\bin\InstallRCBServer-NT.bat"'
FunctionEnd
Using Exec /c instead of nsExec::Exec worked similarly. In the former case, the cmd window shows up briefly and then disappears after the .bat file completes execution. With nsExec, the cmd window never shows up. Interestingly, the /C is needed with nsExec; otherwise the services are not installed.
Since it worked without COMSPEC, I believe the rationale for adding COMSPEC was to ensure compatibility with W2K and XP. However, I'm still having trouble reconciling what I've seen with Takhir's statement that NSIS Exec only works with binary files, and that ExecShell is needed for .bat files. I've read the documentation on Exec and ExecShell, and I'm still not clear on what the distinction is and when to use either of them. The documentation supports Takhir's statement, but my experience seems to bely what the documentation says.
But it is working, and I'm grateful for all the helpful comments.
Anders
17th June 2007 06:42 UTC
Exec uses CreateProcess and ExecShell uses ShellExecute (ExecShell can launch anything, a url, a text file, a pdf)
Afrow UK
17th June 2007 09:22 UTC
You might want to put quotes around $0 in the execution line in case the path contains spaces.
Stu