bat文件来设置附加到路径,如果路径中不包含位置 [英] Bat file to set append to PATH if PATH does not contain location

查看:507
本文介绍了bat文件来设置附加到路径,如果路径中不包含位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个脚本为Windows安装硒服务,还设置了路径,Chrome和IE驱动程序,但每一个脚本运行时间只设置它们(如果​​有的话被更新)最终会碰上追加相同的变量到最后一遍又一遍。
我想这样的:

I am writing a script for windows to install a selenium service that also sets the PATH for the chrome and ie drivers, but just setting them every time the script runs (if anything is updated) will eventually run into appending the same variables to the end over and over. I tried this:

setx IEDRIVER "%~dp0..\ie" /m
setx CHROMEDRIVER "%~dp0..\chrome" /m
if not x%PATH:IEDRIVER=%==x%PATH% setx path "%PATH%IEDRIVER;" /m
if not x%PATH:CHROMEDRIVER=%==x%PATH% setx path "%PATH%CHROMEDRIVER;" /m

但我得到的错误:

but I get the error:

C:\Windows was unexpected at this time.

的最终目标是要检查是否一个变量是在路径中,如果不设置该变量。

The end goal is to check to see if a variable is in the path, if it isn't set that variable.

推荐答案

问题的部分原因是,%PATH%包含空格。当它的可能比较运营商在如果语句两边包含空格,则应该引用这两个参数。

Part of the problem is that %PATH% contains spaces. When it's likely either side of the comparison operator in an if statement contains spaces, you should quote both arguments.

if not "%PATH:substr=repl%"=="%PATH%" do stuff....

接下来,你要提供一个变量作为子到另一个变量。当你这样做,你需要在外部变量延迟扩展。

Next, you're trying to supply a variable as a substring to another variable. When you do this, you need delayed expansion on the outer variable.

setlocal enabledelayedexpansion
if not "!PATH:%IEDRIVER%=!"=="!PATH!" do stuff....
endlocal

现在。而不是检查,看是否PATH中存在的绝对路径,它会是更好地检查你的IE和Chrome目录内的一些文件是否是路径访问。你可以用变量扩展语法做到这一点。

Now. Rather than checking to see whether the absolute paths exist within PATH, it'd be better to check whether some file within your ie and chrome directories are accessible to PATH. You can do this with for variable expansion syntax.

for %%I in (ie chrome) do set %%I=
for %%I in (fileInIE.ext) do set "ie=%%~$PATH:I"
for %%I in (fileInChrome.ext) do set "chrome=%%~$PATH:I"
if defined ie....
if defined chrome...

但最大的问题是,使用 SETX 修改%PATH%是破坏性的,应该避免。含注册表值的PATH有意使用未膨胀的变量,并且在运行时扩展他们一个数据类型被保持。使用 SETX 扩展他们都为时尚早。最好是直接修改注册表,如果你想以编程方式修改你的路径。这并不容易,但它的破坏性更小。

But the biggest issue is that using setx to modify your %PATH% is destructive and ought to be avoided. The registry value containing PATH intentionally uses unexpanded variables, and is held in a data type that expands them at runtime. Using setx expands them all too early. It's better to modify the registry directly if you want to modify your path programmatically. It's not as easy, but it's less destructive.

@echo off
setlocal

for %%I in ("%~dp0..\ie") do set "IEDRIVER=%%~fI"
for %%I in ("%~dp0..\chrome") do set "CHROMEDRIVER=%%~fI"
for %%I in (ie chrome) do set %%I=
for %%I in (fileInIE.ext) do set "ie=%%~$PATH:I"
for %%I in (fileInChrome.ext) do set "chrome=%%~$PATH:I"

if not defined ie call :append_path "%IEDRIVER%"
if not defined chrome call :append_path "%CHROMEDRIVER%"

goto :EOF

:append_path <val>
set "env=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
for /f "tokens=2*" %%I in ('reg query "%env%" /v Path ^| find /i "REG_EXPAND_SZ"') do (

    rem // make addition persistent through reboots
    reg add "%env%" /f /v Path /t REG_EXPAND_SZ /d "%%J;%~1"

    rem // apply change to the current process
    for %%a in ("%%J;%~1") do (path %%~a)
)

rem // use setx to set a temporary throwaway value to trigger a WM_SETTINGCHANGE
rem // applies change to new console windows without requiring a reboot
(setx /m foo bar & reg delete "%env%" /f /v foo) >NUL 2>NUL

color 4E
echo Warning: %%PATH%% has changed.  Reopen the console to inherit the changes.

goto :EOF

这篇关于bat文件来设置附加到路径,如果路径中不包含位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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