What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? 上的答案解释了 system 和 user 环境变量在 Windows 注册表中的存储位置以及如何对其进行更新。它还解释了没有进程可以修改已经运行的进程的环境变量,并且每个新进程都会继承其父进程的当前环境变量列表。
所以在开始 IExpress 安装过程时,此安装过程会从其父进程继承环境变量,该父进程通常是 Windows Explorer,但也可以是 www 浏览器或任何其他应用程序。
IExpress 安装过程运行 msiexec /i node.msi,它安装 Node.js 并很可能添加或修改 system 或 user Windows 注册表中的环境变量。但是对整台机器和当前用户的持久存储环境变量的修改不会自动转移到已经运行的IExpress安装过程的本地环境变量列表中。
接下来由 IExpress 安装过程启动一个带有命令行cmd /c post_install.bat 的命令过程,它通过 Windows 获取当前为 IExpress 安装过程设置的环境变量的新副本一起工作。
因此,无论 Node.js 安装过程在 Windows 注册表中的持久存储 system 和 user 环境变量还是在 上进行了修改MSIEXEC 进程的本地 环境变量对于执行批处理文件post_install.bat 的 Windows 命令进程不可见。
但是post_install.bat执行的npm批处理文件依赖于Node.js安装过程中存储在Windows注册表中的环境变量。因此,在执行npm 批处理之前,有必要使用 system 和 user 环境变量(按此顺序)更新 local 环境变量文件。
这可以通过使用以下注释代码扩展 post_install.bat 来完成:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Set current directory to user's profile directory.
cd /D "%UserProfile%"
rem Make sure the environment variables used to build local PATH from
rem the PATH environment variables currently stored in Windows registry
rem do not already exist with unwanted values.
set "LocalPath="
set "SystemPath="
set "UserPath="
rem Get all system environment variables as currently stored in Windows
rem registry and set them in local environment with exception of system PATH.
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" 2^>nul') do (
if /I "%%A" == "Path" (
set "SystemPath=%%C"
) else if /I "%%B" == "REG_SZ" (
set "%%A=%%C"
) else if /I "%%B" == "REG_EXPAND_SZ" (
call set "%%A=%%C"
)
)
rem Get all user environment variables as currently stored in Windows
rem registry and set them in local environment with exception of user PATH.
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKEY_CURRENT_USER\Environment" 2^>nul') do (
if /I "%%A" == "Path" (
set "UserPath=%%C"
) else if /I "%%B" == "REG_SZ" (
set "%%A=%%C"
) else if /I "%%B" == "REG_EXPAND_SZ" (
call set "%%A=%%C"
)
)
rem PATH can contain references to environment variables which can be
rem expanded only after having all environment variables except system
rem and user PATH already set in local environment. Now it is possible
rem to expand the environment variable references in system and user
rem PATH and concatenate them two one PATH set in local environment
rem replacing PATH as inherited from process starting this batch file.
if not defined SystemPath goto ProcessUserPath
call set "LocalPath=%SystemPath%"
if not "%LocalPath:~-1%" == ";" set "LocalPath=%LocalPath%;"
:ProcessUserPath
if not defined UserPath goto SetLocalPath
call set "LocalPath=%LocalPath%%UserPath%"
:SetLocalPath
if not defined LocalPath goto DoInstall
if "%LocalPath:~-1%" == ";" set "LocalPath=%LocalPath:~0,-1%"
if defined LocalPath set "PATH=%LocalPath%"
:DoInstall
rem Call NPM in its own environment inherited from current local environment.
rem Then discard all modifications made by NPM batch file on its own local
rem environment and restore the environment this batch file has set before.
setlocal
call npm.cmd install ...
endlocal
rem Insert here other post installation commands.
rem Restore previous environment on starting this batch file which means
rem discarding all modifications made on local list of environment variables
rem and restoring initial current directory before modification by CD at top
rem and restoring initial status of command extensions and delayed expansion.
endlocal
此批处理文件读取当前存储在 Windows 注册表中的 system 和 user 环境变量,并更新 local 环境变量,包括 local PATH 甚至可以在 Windows XP / Windows Server 2003 上工作,尽管与 Windows Vista / Server 2008 和所有更高版本的 Windows 相比,REG 在 Windows XP / Server 2003 上的输出有所不同。
要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。
call /?
cd /?
echo /?
endlocal /?
for /?
if /?
reg /?
reg query /?
rem /?
set /?
setlocal /?