change - node.js command prompt commands
change directory command cd..not working in batch file after npm install (1)
I have the following command in one bat file.
echo STEP12 cd HDC git config --global url."https://".insteadOf git:// echo STEP13 cd hui-components-style npm install --registry http://localhost:23510 cd ..
In STEP13, after npm install command
is not working. Its not going back to the parent HDC folder. i have other commands to run in the parent folder.Am I making some syntax errors?
is on Windows a Windows batch script with file extension
and not an executable which in this case modifies current directory and does not restore it before exiting.
I suggest to use instead of
and use instead of
For details on the two commands – push and pop directory – open a command prompt window and run
to get displayed the help for each command.
An explanation for better understanding using absolute paths.
The current directory is
C:\Temp\HDCon stack and sets as new current directory
npmis executed which modifies the current directory.
C:\Temp\HDCfrom stack and sets this directory as current directory independent on which directory is the current directory.
So the code with those two modifications is:
echo STEP12 cd HDC git config --global url."https://".insteadOf git:// echo STEP13 pushd hui-components-style call npm.cmd install --registry http://localhost:23510 popd
It is necessary to use command
is a batch file with complete file name
and not an executable, i.e.
call npm.cmd install --registry http://localhost:23510
Otherwise the command processing of the current batch file is
and whatever commands are in current batch file after the line with
are never processed by Windows command processor. For details about the various methods to execute a batch file see answer on
How to call a batch file that is one level up from the current directory?
. And see also answer on
copy command in batch file is not getting executed when calling the batch file from another batch file, but is getting executed when I double click
Alternatively it would be also possible to use following code:
echo STEP12 cd HDC git config --global url."https://".insteadOf git:// echo STEP13 cd hui-components-style setlocal call npm.cmd install --registry http://localhost:23510 endlocal cd ..\
The command setlocal does following:
- It pushes path of current directory on stack.
- It pushes state of command extensions on stack.
- It pushes state of delayed expansion on stack.
- It pushes the memory address of the current environment variables table on stack.
- It creates a copy of the current environment variables table in memory and makes this new environment variables table active.
Those 5 steps are always done even with
being called with 1 or 2 of the 4 possible options
change state of command extensions and/or delayed environment variable expansion.
Now batch file
can change the current working directory, can add, delete and modify environment variables, can enable/disable command extensions, and can enable/disable the usage of delayed expansion.
But all those modifications on command processing environment don't matter after next command endlocal because endlocal
- deletes the current environment table;
- pops memory address of previous environment table from stack and uses this address resulting in restoring initial environment variables;
- pops state of delayed expansion from stack and disables/enables delayed expansion accordingly;
- pops state of command extensions from stack and disables/enables command extensions accordingly;
- pops previous current directory path from stack and sets current directory to this path to restore the current directory.
For examples which demonstrate that see the answers on
- Why is my cd %myVar% being ignored? (example for current working directory management)
- Echoing a URL in Batch (example for environment variable and delayed expansion management)
The names of the two commands are actually self-explaining:
- setlocal ... setup local command process environment based on current environment.
- endlocal ... end local command process environment and restore previous environment.