更改目录命令 cd .. 在 npm 安装后不在批处理文件中工作 [英] change directory command cd ..not working in batch file after npm install

查看:22
本文介绍了更改目录命令 cd .. 在 npm 安装后不在批处理文件中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 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 install 命令 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 上是一个带有文件扩展名 .cmd 的 Windows 批处理脚本,而不是一个可执行文件本例修改当前目录,退出前不恢复.

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 directory – 打开命令提示符窗口并运行 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.

  1. 当前目录为C:TempHDC.
  2. 命令 pushd hui-components-styleC:TempHDC 保存在堆栈上并设置为新的当前目录 C:TempHDChui-components-style.
  3. npm 被执行,修改当前目录.
  4. 命令 popd 从堆栈中获取 C:TempHDC 并将此目录设置为当前目录,独立于哪个目录是当前目录.
  1. The current directory is C:TempHDC.
  2. The command pushd hui-components-style saves C:TempHDC on stack and sets as new current directory C:TempHDChui-components-style.
  3. npm is executed which modifies the current directory.
  4. The command popd gets C:TempHDC 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继续,并且在 npm 行之后的当前批处理文件中的任何命令code> 永远不会被 Windows 命令处理器处理.有关执行批处理文件的各种方法的详细信息,请参阅 如何调用当前目录上一级的批处理文件的答案? 并参见 从另一个批处理文件调用批处理文件时,批处理文件中的复制命令没有得到执行,而是当我双击时被执行.

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 执行以下操作:

The command setlocal does following:

  1. 它将当前目录的路径压入堆栈.
  2. 它将命令扩展的状态推送到堆栈上.
  3. 它将延迟扩展的状态推送到堆栈上.
  4. 它将当前环境变量表的内存地址压入堆栈.
  5. 它在内存中创建当前环境变量表的副本,并使这个新的环境变量表处于活动状态.

即使使用 EnableExtensionsDisableExtensions 四个可能选项中的一个或两个调用 setlocal,也总是会完成这五个步骤>EnableDelayedExpansionDisableDelayedExpansion另外更改命令扩展和/或延迟环境变量扩展的状态.

Those five steps are always done even with setlocal being called with one or two of the four 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 execution environment don't matter after next command endlocal because endlocal

  1. 删除当前环境表;
  2. 从堆栈中弹出先前环境表的内存地址,并使用该地址恢复初始环境变量;
  3. 从堆栈弹出延迟扩展状态并相应地禁用/启用延迟扩展;
  4. 从堆栈中弹出命令扩展的状态并相应地禁用/启用命令扩展;
  5. 从堆栈中弹出上一个当前目录路径并将当前目录设置为此路径以恢复当前目录.

有关证明的示例,请参阅

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 ... 根据当前环境设置本地执行环境.
  • endlocal ...结束本地执行环境并恢复之前的环境.
  • setlocal ... setup local execution environment based on current environment.
  • endlocal ... end local execution environment and restore previous environment.

这篇关于更改目录命令 cd .. 在 npm 安装后不在批处理文件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆