I am creating a batch file to siliently install nodejs on a Windows XP machine.
I am trying to automate the installation of node module dependencies (npm install).
I would normally issue npm install from the cmd prompt in the target installation directory.
I am struggling to automate the interaction with the command prompt from a batch file.
The following line in my batch script seems to make it possible for me to pipe a text file of commands to cmd:
for /F "usebackq delims=," %%i in ("c:\foo\source\npm_install.txt") do echo %%i | "c:\windows\system32\cmd.exe"
The batch file is located in c:\foo\source. I need to issue ‘npm install’ from c:\foo\bin.
If my npm_install.txt file is such:
cd /d c:\foo\bin,
npm install
The cmd prompt will perform the first command changing the directory from c:\ to c:\foo\bin.
It will then perform the second command but starting from c:\ again. The previous command to change directories doesn’t persist. It seems every command in the text file will be issued from c:\ .
I next tried to issue both commands from a combined statment:
cd /d c:\foo\bin && npm install
It seems this approach will allow me to overcome the prior path problem but I then have an issue with the space between npm and install.
The cmd prompt performs c:\foo\bin>npm and causes npm to trip on the space.
I have tried enclosing the command without success: ‘npm install’, “npm install”, (npm install).
Can anyone tell me what I am doing wrong?
Thanks.
You do not need this:
do echo %%i | "c:\windows\system32\cmd.exe". Simply put your commands in a block.With your previous statement, you start new cmd interpreter, ask it to execute a command for you and exit – that’s why you loose effect of that
cd.If you do not specify tokens in for loop, only 1st is read. Plus, all tokens must be on same line (I’m not sure if what you show is not a byproduct of formatting)
Use
"delims="to read full line.Do not mix commands with arguments if you do not have to: put only directories in your file:
so finally it becomes (I replaced cd with pushd/popd so you’ll end up in the same dir you started from):
Edit: if
npm installis batch itself, you will need to use call, as ebohlman noted