Bat file to set append to PATH if PATH does not contain location [duplicate]

落花浮王杯 提交于 2020-01-05 07:59:05

问题


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.


回答1:


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

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...

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



回答2:


Setx refuses to add an existing path to the path.



来源:https://stackoverflow.com/questions/29438706/bat-file-to-set-append-to-path-if-path-does-not-contain-location

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!