npm安装后更改目录命令cd ..不能在批处理文件中工作 [英] change directory command cd ..not working in batch file after npm install
问题描述
我在一个bat文件中有以下命令.
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 ..
在STEP13中,在npm安装命令cd..
之后不起作用.它不会返回到父HDC文件夹.我还有其他命令要在父文件夹中运行.我是否犯了一些语法错误?
In STEP13, after npm install command cd..
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?
推荐答案
npm
在Windows上是Windows批处理脚本,文件扩展名为.cmd
,并且不是可执行文件,在这种情况下,该可执行文件会修改当前目录,并且之前不会还原该目录.退出.
npm
is on Windows a Windows batch script with file extension .cmd
and not an executable which in this case modifies current directory and does not restore it before exiting.
我建议使用代替
cd hui-components-style
命令
pushd hui-components-style
并使用代替
cd ..
命令
popd
有关这两个命令(push和pop目录)的详细信息,请打开命令提示符窗口并运行pushd /?
和popd /?
以显示每个命令的帮助.
For details on the two commands – push and pop directory – open a command prompt window and run pushd /?
and popd /?
to get displayed the help for each command.
使用绝对路径可以更好地理解的说明.
An explanation for better understanding using absolute paths.
- 当前目录为
C:\Temp\HDC
. - 命令
pushd hui-components-style
将C:\Temp\HDC
保存在堆栈上,并设置为新的当前目录C:\Temp\HDC\hui-components-style
.
执行 -
npm
会修改当前目录. - 命令
popd
从堆栈中获取C:\Temp\HDC
,并将该目录设置为当前目录,而与当前目录是哪个目录无关.
- The current directory is
C:\Temp\HDC
. - The command
pushd hui-components-style
savesC:\Temp\HDC
on stack and sets as new current directoryC:\Temp\HDC\hui-components-style
. npm
is executed which modifies the current directory.- The command
popd
getsC:\Temp\HDC
from 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
必须使用命令call
,因为npm
是具有完整文件名npm.cmd
的批处理文件,而不是可执行文件,即
It is necessary to use command call
because of npm
is a batch file with complete file name npm.cmd
and not an executable, i.e.
call npm.cmd install --registry http://localhost:23510
否则,当前批处理文件的命令处理在npm.cmd
上继续继续,并且Windows命令处理器从不处理带有npm
的行之后当前批处理文件中的任何命令.有关执行批处理文件的各种方法的详细信息,请参见如何调用从当前目录向上一级的批处理文件? .并参见复制命令,从另一个批处理文件调用该批处理文件时,批处理文件中的复制命令未得到执行,但当我执行该操作时,该命令已被执行双击.
Otherwise the command processing of the current batch file is continued on npm.cmd
and whatever commands are in current batch file after the line with npm
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 ..\
命令 setlocal 执行以下操作:
- 它将当前目录的路径压入堆栈.
- 它将命令扩展的状态推送到堆栈上.
- 它将延迟扩展状态推入堆栈.
- 它将当前环境变量表的内存地址压入堆栈.
- 它将在内存中创建当前环境变量表的副本,并使该新环境变量表处于活动状态.
即使使用4个可能的选项中的1个或2个来调用setlocal
,也要始终执行这5个步骤,而EnableExtensions
,DisableExtensions
,EnableDelayedExpansion
,DisableDelayedExpansion
则要进行另外更改命令扩展的状态和/或延迟的环境变量扩展.
Those 5 steps are always done even with setlocal
being called with 1 or 2 of the 4 possible options EnableExtensions
, DisableExtensions
, EnableDelayedExpansion
, DisableDelayedExpansion
to additionally change state of command extensions and/or delayed environment variable expansion.
现在批处理文件npm.cmd
可以更改当前工作目录,可以添加,删除和修改环境变量,可以启用/禁用命令扩展,以及启用/禁用延迟扩展.
Now batch file npm.cmd
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.
但是在下一个命令 endlocal 之后,对命令处理环境的所有修改都无关紧要,因为 endlocal
But all those modifications on command processing environment don't matter after next command endlocal because endlocal
- 删除当前环境表;
- 从堆栈中弹出先前环境表的内存地址,并使用该地址来恢复初始环境变量;
- 从堆栈中弹出延迟扩展的状态,并相应地禁用/启用延迟扩展;
- 从堆栈中弹出命令扩展的状态,并相应地禁用/启用命令扩展;
- 从堆栈中弹出先前的当前目录路径,并将当前目录设置为此路径以恢复当前目录.
有关展示示例的示例,请参见
For examples which demonstrate that see the answers on
- 为什么我的CD%myVar%会被忽略?(当前工作目录管理的示例)
- 批量返回URL (环境变量和延迟扩展管理的示例)
- 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 ...根据当前环境设置本地命令处理环境.
- endlocal ...结束本地命令进程环境并还原以前的环境.
- setlocal ... setup local command process environment based on current environment.
- endlocal ... end local command process environment and restore previous environment.
这篇关于npm安装后更改目录命令cd ..不能在批处理文件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!