Tuesday, May 5, 2015

Running multiple commands from single batch file in Windows

If your batch file is having multiple commands and those commands are trying to execute or invoke other batch commands something like this.

Example:
SET PATH=%PATH%;%PROGRAMFILES%\nodejs;%PROGRAMFILES(x86)%\nodejs;%APPDATA%\npm
npm install -g mypackageinstaller
mypackageinstaller use latest


If you run above commands from a batch file, only first 2 commands 
will be get executed and the last one will be ignored.

Reason being, here npm is a batch file from Node, once windows command process invokes npm process,after npm get executed it will not return to the original command process. Hence,third command won't be executed from a batch file.

To avoid these issues, we can invoke these kind of commands using 
"call".

Modified batch script:

SET PATH=%PATH%;%PROGRAMFILES%\nodejs;%PROGRAMFILES(x86)%\nodejs;%APPDATA%\npm
call npm install -g mypackageinstaller
call mypackageinstaller use latest

All these commands will be executed sequentially.

To make them parallel, we can use "start" command.

No comments:

Post a Comment