简体   繁体   English

Windows路径不包含C:\\ WINDOWS \\ system32,可以使用批处理脚本添加它吗?

[英]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.

相关问题 如何在Windows 8的路径中添加C:\\ windows \\ system32? - How to add C:\windows\system32 to path on Windows 8? 假设路径C:\ WINDOWS \ system32始终存在是否安全? - Is it safe to assume that the path C:\WINDOWS\system32 always exists? 使用批处理文件将文件从本地文件夹复制到“C:\\ Windows \\ System32 \\” - Copying a file from local folder to “C:\Windows\System32\” using batch file Python无法在C:\\ Windows \\ System32 \\ oobe \\ Info \\ backgrounds中打开文件 - Python can't open a file in C:\Windows\System32\oobe\Info\backgrounds 如何使用命令提示符或批处理文件在主机文件(位于C:/ windows / system32 / drivers / etc文件夹中)中设置文本? - How to set texts in a hosts file (found in C:/windows/system32/drivers/etc folder) using command prompt or a batch file? 从任务计划程序启动批处理文件指向 C:\Windows\system32 - Starting batch file from Task scheduler point to C:\Windows\system32 os.path.exists 无法识别 C:\Windows\system32\drivers 下的子目录 - os.path.exists does not recognize a subdirectory under C:\Windows\system32\drivers C++ 重命名文件@C:\\Windows\\System32\\Drivers - C++ Rename File @ C:\Windows\System32\Drivers 任何人都可以帮助为什么在控制台或 Winform 中这将获取用户目录,但我在服务程序中使用它已更改为 c:/Windows/System32 - Anyone can help why in Console or Winform this will get user directory, but I use it in service program been changed to c:/Windows/System32 Puppet 无法在 C:\\windows\\system32 文件夹中创建文件 - Puppet is not able to create files in C:\windows\system32 folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM