0

I am attempting to query the systems PATH variable in preparation of an update.

  • If the old string exists, remove and append on new strings (x2) to beginning of the path.
  • If the new strings are already in the path, then do nothing
  • If neither the new or old are in the path, then append on new to beginning of the path.

I have wrote a batch, but it will break if the new path is already appended when I dont first clear out %errorlevel, OR if I clear the %errorlevel, it will simply add the new path on regardless if it is already there or not. I am sure there is an easier way to do this.

@echo off
setlocal enableextensions disabledelayedexpansion
type nul > C:\_sandbox\%computername%_PATH_UPDATE.log
echo %DATE% - %TIME% - PATH UPDATE started >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo. >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo Checking for OLD_PATH >> C:\_sandbox\%computername%_PATH_UPDATE.log
set "ErrorLevel="
echo %PATH% | FINDSTR /IX  "C:\Program Files (x86)\OLD_PATH"
If %ErrorLevel%==0 goto 0
If %ErrorLevel%==1 goto 1

:0
set "ErrorLevel="
echo OLD_PATH found, removing and appending new path  >> C:\_sandbox\%computername%_PATH_UPDATE.log
set NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:;C:\Program Files (x86)\OLD_PATH=%
setx PATH "%NEWPATH%" /m
exit /b 0

:1
set "ErrorLevel="
echo. >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo OLD_PATH not found, checking for NEW_PATH  >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo %PATH% | FINDSTR /IX  "C:\Program Files (x86)\NEW_PATH2"
If %ErrorLevel%==0 goto 2
If %ErrorLevel%==1 goto 3

:2
set "ErrorLevel="
echo NEW_PATH found, no further action required  >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo %path%
exit /b 0

:3
set "ErrorLevel="
echo NEW_PATH nor OLD_PATH were found, appending NEW_PATH  >> C:\_sandbox\%computername%_PATH_UPDATE.log
set NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:;C:\Program Files (x86)\OLD_PATH=%
setx PATH "%NEWPATH%" /m
echo NEW_PATH Should of been appended.  >> C:\_sandbox\%computername%_PATH_UPDATE.log
exit /b 0

I do not NEED it logged, however for testing I have been logging everything instead of using pauses as logging may prove useful for deployments. The Batch will be ran as SYSTEM which I imagine will require the users to relog.

In our instance, OLD PATH (FOR EXAMPLE) is C:\Program Files (x86)\Microsoft\Notepadv1.2 - Where as NEW_PATH is C:\Program Files (x86)\Microsoft\Notepad and C:\Program Files (x86)\Microsoft\Notepad_x64 respectively. That is why I query for NEW_PATH2 using /IX flags in FINDSTR.

  • Please see [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) This code is __really__ not good friendly spoken. Never add a folder to __system__ `PATH` left to the standard Windows folders. Never use __local__ `PATH` to build a new __system__ `PATH`. Never replace __user__ or __system__ `PATH` with environment variable references by a string with all environment variable references expanded. All those "never" can result in other applications suddenly not working anymore. – Mofi Jan 30 '18 at 07:49
  • Please take also a look on [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/a/41461002/3074564) and [How can I use a .bat file to remove specific tokens from the PATH environment variable?](https://stackoverflow.com/a/38664286/3074564) and [How to search and replace a string in environment variable PATH?](https://stackoverflow.com/a/24650324/3074564) and [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564). – Mofi Jan 30 '18 at 07:55

2 Answers2

0

Try this:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

TYPE nul > C:\_sandbox\%computername%_PATH_UPDATE.log
ECHO %DATE% - %TIME% - PATH UPDATE started >> C:\_sandbox\%computername%_PATH_UPDATE.log
ECHO. >> C:\_sandbox\%computername%_PATH_UPDATE.log
ECHO Checking for OLD_PATH >> C:\_sandbox\%computername%_PATH_UPDATE.log

ECHO %PATH% | FINDSTR /I /C:"C:\Program Files (x86)\OLD_PATH"
IF %ERRORLEVEL%==0 GOTO 0
IF %ERRORLEVEL%==1 GOTO 1

:0
ECHO OLD_PATH found and removed, NEW_PATH appended  >> C:\_sandbox\%computername%_PATH_UPDATE.log
SET NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:C:\Program Files (x86)\OLD_PATH;=%
SETX PATH "%NEWPATH%" /m
EXIT /B

:1
ECHO. >> C:\_sandbox\%computername%_PATH_UPDATE.log
ECHO OLD_PATH not found, checking for NEW_PATH  >> C:\_sandbox\%computername%_PATH_UPDATE.log
ECHO %PATH% | FINDSTR /I /C:"C:\Program Files (x86)\NEW_PATH2"
IF %ERRORLEVEL%==0 GOTO 2
IF %ERRORLEVEL%==1 GOTO 3

:2
ECHO NEW_PATH found, no further action required  >> C:\_sandbox\%computername%_PATH_UPDATE.log
ECHO %PATH%
EXIT /B

:3
ECHO NEW_PATH nor OLD_PATH were found, NEW_PATH appended  >> C:\_sandbox\%computername%_PATH_UPDATE.log
SET NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:C:\Program Files (x86)\OLD_PATH;=%
SETX PATH "%NEWPATH%" /m
EXIT /B

FINDSTR /C: is what you were searching for.


Also, you should never attempt to write to the %ERRORLEVEL% variable because the value you set will create a user variable named ERRORLEVEL which then takes precedence over the internal pseudo variable ERRORLEVEL (read more).

If you need a specific ERRORLEVEL you can make use of:

  • CALL SET for an ERRORLEVEL of 0
  • COLOR 00 for an ERRORLEVEL of 1
  • EXIT /B n for an ERRORLEVEL of n

Ironically your attempt to reset the ERRORLEVEL does not create a user variable nor change the ERRORLEVEL.

Community
  • 1
  • 1
FatalBulletHit
  • 762
  • 6
  • 22
  • Please don't try this code, see [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) This code is __really__ not good friendly spoken. Never add a folder to __system__ `PATH` left to the standard Windows folders. Never use __local__ `PATH` to build a new __system__ `PATH`. Never replace __user__ or __system__ `PATH` with environment variable references by a string with all environment variable references expanded. All those "never" can result in other applications suddenly not working anymore. – Mofi Jan 30 '18 at 07:48
  • Tbh, I only tried to get the code working, don't know a lot about `PATH`... but thanks for the info! :) – FatalBulletHit Jan 30 '18 at 22:45
0

Got it working. Updated for anyone's future searches.

  • path.old = old path
  • path = new path

Our old path had a version number in it for an in house product. The new path no longer has the version number. They are similar, which caused all of this hassle.

@echo off
setlocal enableextensions disabledelayedexpansion
ver > nul
echo %path% | FINDSTR /I ".*old.*"
If %ErrorLevel%==0 goto 0
If %ErrorLevel%==1 goto 1

:0
ver > nul
set NEWPATH=C:\Program Files (x86)\path_x64;C:\Program Files (x86)\path;%PATH:;C:\Program Files (x86)\path.old=%
setx /m PATH "%NEWPATH%"
exit

:1
ver > nul
echo %path% | FINDSTR /I ".*path_x64"
If %ErrorLevel%==0 exit
If %ErrorLevel%==1 goto 2

:2
set NEWPATH=C:\Program Files (x86)\newpath_x64;C:\Program Files (x86)\newpath;%PATH%
setx /m PATH "%NEWPATH%"
exit