[英]Windows path doesn't contain C:\WINDOWS\system32, can I add it using batch script?
I am writing a batch script that, quite reasonably, depends on "C:\\WINDOWS\\system32" being part of the PATH environment variable. 我编写的批处理脚本相当合理地依赖于PATH环境变量中的“ C:\\ WINDOWS \\ system32”。 I recently encountered a (developer's) machine that had a really weird path that didn't include system32, and therefore my batch script didn't work.
我最近遇到了一台(开发人员)的机器,它的路径很奇怪,没有包含system32,因此我的批处理脚本不起作用。
I looked up ways for my batch script to check the PATH variable and add system32 if it is not there. 我查找了批处理脚本检查PATH变量并添加system32(如果不存在)的方法。 However, the solution I found used
setx
which ironically enough ALSO depends on system32 in the PATH variable. 但是,我发现的解决方案使用了
setx
,具有讽刺意味的是,ALSO也取决于PATH变量中的system32。 Are there any programmatic ways to add system32 to the PATH without it already being there? 有没有将system32添加到PATH的任何编程方式?
Also please let me know if this is such an edge case that it doesn't make sense to make my script robust against it. 另外请让我知道这是否是一种极端情况,以至于使我的脚本不易对付它。 I'm not expecting any of my typical users to have such a borked PATH variable.
我不希望我的任何典型用户都拥有如此讨厌的PATH变量。 Should I bother?
我应该打扰吗?
try this: 尝试这个:
for /f "delims=" %%a in ("%comspec%") do set "PATH=%PATH%;%%~DPa"
or this: 或这个:
for /f "delims=" %%a in ("%comspec%") do set "compath=%%~DPa"
set "PATH=%PATH%;%compath:~0,-1%"
@ECHO OFF
SETLOCAL
SET "required=c:\windows\system32"
for %%p in ("%path:;=" "%") do (
FOR %%j IN ("" \) DO (
IF /i %%p=="%required%%%~j" GOTO :nextstep
)
)
SETx PATH "%required%;%path%"
:nextstep
ECHO PATH=%path%
GOTO :EOF
Here's my take on the problem. 这是我对这个问题的看法。
PATH
may contain the required directory with or without a trailing \\
, and it may or may not have a preceding or trailing ;
PATH
可能包含必需的目录,带有或不带有尾随\\
,并且它可能具有也可能没有前导或尾随;
I'd suggest that you examine the operation of SETX
however. 我建议您检查一下
SETX
的操作。 It sets an environment variable for FUTURE cmd sessions, not the CURRENT or EXISTING sessions, AFAIAA....and perhaps not for PATH
or some other variables (I tried setting PATH
using the SETX
in the above batch - future sessions did NOT acquire the new value set, but it appeared to be set according to regedit32
- perhaps it needs a reboot - haven't investigated further at this stage) 它为未来CMD会话,而不是当前的或现有的会话,AFAIAA ....或许不是一个环境变量
PATH
或其他一些变量(我尝试设置PATH
使用SETX
在上面的批处理-未来的会议并没有取得新值集,但它似乎是根据regedit32
设置的-也许需要重新启动-目前尚未进行进一步调查)
Note that the above will need to have the required directory appended to %path%
if my observations are borne out... 请注意,如果我的观察得到证实,以上内容将需要的目录附加到
%path%
后面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.