How to set PATH environment variable in batch file only once on Windows? -
i have batch file sets user path , run part of visual studio ide build step.
@echo off @echo %path% set comspec = "%vcinstalldir%\vcvarsall.bat" amd64 setx path "..\..\lib\libsndfile;..\..\lib\simulink" @echo %path%
when build project, close vs, , reopen it, , rebuild, see appended path part of path
variable. however, see in windows setting of environment variable path
variable created under user environment variables as
..\..\lib\libsndfile;..\..\lib\simulink
question 1:
why path appear appended path part of system environment variable?
on executing echo %path%
through visual studio console (when run project second times) prints system variable path , new path created appended it.
question 2:
i want modify batch file sets once path
environment variable in user settings during first run of visual studio build. if user variable path
exists on subsequent runs, should not execute set command again avoid appending new path again , again in system variable.
any ideas how achieve this?
edit: after testing, appears original answer isn't entirely applicable op's questions. answer op more directly:
%path%
combines values inhklm\system\currentcontrolset\control\session manager\environment\path
hkcu\environment\path
. whensetx "dir;dir"
, you're settinghkey_current_user
path
value. machine-widehkey_local_machine
path
value remains untouched. that's why see values appended, rather replacements. you'd have usesetx /m
replacehklm
path
value. but please don't unless want create severe problems operating system installation.if want test whether directory exists in
%path%
,cd
orpushd
both directory want check , each directory within%path%
unify each, making sure relative paths, environment variables, etc. flattened.set "var=%cd%"
each.if /i "!dir1!"=="!dir2!"
directory exists somewhere in%path%
. there's example of in original answer below.
the reason original answer isn't entirely applicable because setx
isn't destructive once thought. danger times when users want append directory path, they'll setx /m path "%path%;new dir"
; , is destructive. because %path%
expanded before setx
writes value, directories in path expanded prematurely.
the following method safer:
set "env=hklm\system\currentcontrolset\control\session manager\environment" /f "tokens=2*" %%i in ( 'reg query "%env%" /v path ^| findstr /i "\<path\>"' ) setx /m path "%%j;new directory"
but wasn't op asked, , apologize knee-jerk answer.
original answer: setx
destructive , shouldn't used way. when setx path
you're converting registry value data type reg_expand_sz reg_sz. this, dynamic environment variables stored in %path% converted flat, absolute paths. use path
command append directories %path%
temporarily, , reg add
permanently. (as side note, there's dpath
, temporarily adds directory path, can used type
command. scroll 2/3 way down this page more info on dpath
.)
here's utility script wrote add directories %path%
in less destructive manner. avoid adding same directory %path%
more once, regardless of how it's formatted (e.g. trailing backslash, relative paths, environment variables, or other permutation).
@echo off setlocal enabledelayedexpansion if not exist "%~1" goto usage %%i in ("%~1") pushd "%%~i" 2>nul && (set "new=!cd!" && popd) || goto usage %%i in ("%path:;=";"%") pushd "%%~i" 2>nul && ( rem // delaying expansion of !new! prevents parentheses breaking things if /i "!new!"=="!cd!" ( echo !new! exists in %%path%% goto :eof ) popd ) call :append_path "%new%" goto :eof :usage echo usage: %~nx0 "dir" goto :eof :append_path <val> set "env=hklm\system\currentcontrolset\control\session manager\environment" /f "tokens=2*" %%i in ('reg query "%env%" /v path ^| findstr /i "\<path\>"') ( rem // make addition persistent through reboots reg add "%env%" /f /v path /t reg_expand_sz /d "%%j;%~1" rem // apply change current process %%a in ("%%j;%~1") path %%~a ) rem // use setx set temporary throwaway value trigger wm_settingchange rem // applies change new console windows without requiring reboot (setx /m foo bar & reg delete "%env%" /f /v foo) >nul 2>nul color 4e echo warning: %%path%% has changed. reopen console inherit changes. goto :eof
Comments
Post a Comment