Skip to content
⌘ NSIS Forum Archive

Weird behaviour occurring when executing cmd file with ExecWait/nsExec/Exec

7 posts

green_biri#

Weird behaviour occurring when executing cmd file with ExecWait/nsExec/Exec

Hi everyone, I'm trying to execute a cmd file on my NSIS script.

The cmd file contains only the following instructions to setup NodeJS:


call npm install -g appium
call npm install -g appium-doctor
In order to run the script, I've tried to use ExecWait, nsExec::Exec and Exec. Currently I'm using ExecWait, since every command is behaving the same way. Here is the way I'm calling the script:


ExecWait '"$sysdir\cmd.exe" /C if 1==1 "$INSTDIR\npm_config.cmd"'
The problem is, the instructions in the cmd file will only be executed the second time I run the installer.

To further explain:

- I have the ExecWait in a separate section.

- If I run the installer for the first time and choose the section, the cmd does not execute (cmd window just blinks and error code is 1). If I run the installer a second time and choose the section, the instructions will execute without any issue (cmd window opens and executes everything like expected).

- If I don't choose the section on the first time the installer is ran, obviously it won't execute my cmd. However, if I reopen the installer and then select the section, it will execute the cmd correctly.

So, to resume, I need to open and reopen my installer in order for the cmd instructions to be executed correctly.

What could be causing this behaviour? How can I debug the issue?
green_biri#
Apparently I cannot edit my post : (

I've discovered that reopening the installer will not work.

This is my current workaround:

- Open installer
- Install other section
- Close installer and reopen
- Select section where cmd is called
- Cmd works
Anders#
Whatever is going on is not NSIS, must be something in the batch.

Change /C to /K or add "pause" to the end of your batch-file so you can see the output of your batch...
green_biri#
Hi Anders,

Thank you for the suggestion! Apparently I get the generic error "npm is not a recognized command/file", which means that the installer does not have the correct environment variables after running the NodeJS setup in unattended mode (i'm installing JDK and NodeJS before running the cmd).

So I guess the question now is: How do I re-read/reload the environment variables during the installation? Is there any way to do it?

Thank you once again.
Anders#
The easy solution is to just prepend the batch file with "set path=%path%;$nodeinstallpath".

There is a undocumented Windows function you can call to update the in-process environment but I don't remember its name off the top of my head.
green_biri#
Thanks Anders!

I believe the function you are mentioning is this one (found it here):

System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("PATH", R0).r0'
After adding it to my script, I was able to fix the problem. Thank you once again for helping me reach the solution! 🙂