How to get existing environment variable, change the value of it and display it after change with CMD command?

So, after discussing with Berend, I figured out that I need to explain myself better and that is what I'm going to do. So, my mission is to write a windows batch script that will:

  1. Display(print to screen) the value of anyone from the environment variables I have(but maybe I need to check if it exists, and if not I need to create one? I'm not sure) 1.1 If I need to check if the variable exists, there is any if or for loop that I can do to check it? If not, I think that I'll just create new environment variable and work with it.

  2. I need to update the value of the previous variable I just printed.

  3. Display the value of the current variable After the update I just did in step 2.

I hope now it's more clear than before and I'll be glad for any help.

9

3 Answers

From your comment I understand that you want to change one directory in the PATH variable.

For instance, if PATH is C:\WINDOWS\system32;C:\WINDOWS;C:\MyFolder it needs to be changed to C:\WINDOWS\system32;C:\WINDOWS;C:\MyOtherFolder

You can do that like this:

echo %PATH%
set PATH=%PATH:MyFolder=MyOtherFolder%
echo %PATH%

The syntax is %VARIABLE:SEARCH=REPLACE%

20

To check if a variable exist it is very simple:

IF Defined SomeVariable (Echo The Variable Exists) else (echo The Variable does not Exist)

To check if a variable has an empty value:

if "%SomeVariable%"=="" (Echo the Variable is Empty) else (echo The Variable is not Empty)

To see if a simple variable (one entry variable) has a specific value and if not change it to what you want:

if not "%SomeVariable%"=="ValueIWant" Setx SomeVariable "ValueIWant"

Some as above but no permanent change:

if not "%SomeVariable%"=="ValueIWant" Set SomeVariable=ValueIWant

To change the user or system %path% variable it is a little more complicated because this variable can have several entries separated by semi-column, here is an example of a batch that changes de user %path% variable to a new value:

what you have to do is change the the lines in:

set OldEntry=

set NewEntry=

I added a lot comments in the batch so you know what is going on.

Here in this video I show it in action Youtube

Code:

@echo off
setlocal EnableDelayedExpansion
:: The OldEntry is the Entry that is already in your user %path% and the NewEntry is the New entry that is going to substitute OldEntry
set OldEntry=C:\Program Files\CLI\Speedtest
set NewEntry=C:\Mybanana
:: Saves current User %Path% in UserPath variable:
For /f "tokens=2*" %%a in ('reg query "HKCU\Environment" /v "Path"') do set "UserPath=%%b"
echo.
:: check if OldEntry exists in user %path% and if yes substitute OldEntry by NewEntry:
for /f "delims=" %%a in ('"echo %UserPath:;=&echo.%"') do if /i "%%a"=="%OldEntry%" ( set "NewUserPath=!UserPath:%OldEntry%=%NewEntry%!" setx Path "!NewUserPath!" 1> nul 2> nul )
:: IF Variable NewUserPath does not exist that means there is no OldEntry in the current path and thus gives error:
IF not Defined NewUserPath goto :ErrorNotFound
:: IF success change was made a new window opens showing current and old Path...
For /f "tokens=2*" %%a in ('reg query "HKCU\Environment" /v "Path"') do set "NewUserPath=%%b"
start "" "%windir%\system32\cmd.exe" /c "( echo Old user %%path%%: echo. echo %UserPath:;=&echo.% echo. echo. echo Current user %%path%%: echo. echo %NewUserPath:;=&echo.% echo. echo. echo OldEntry = "%OldEntry%" echo NewEntry = "%NewEntry%" echo. pause )"
exit
:ErrorNotFound
echo The Entry "%OldEntry%" was not found in the user %%path%%^^!
echo.
pause
exit
12

To add or change a number in a variable:

@echo off
:: Create a variable with a value of 5
set Number=5
echo Step 01: Number: %Number%
echo.
:: Change the value form 5 to 7
set Number=7
echo Step 02: Number: %Number%
echo.
:: Using matematical operations to add 3 to the current value of the variable %Number%
set /a Number=%Number% + 3
echo Step 03: Number: %Number%
echo.
:: Add the value of Number to another variable:
set SomeVariable=%Number%
echo Step 04: SomeVariable: %SomeVariable%
echo.
pause
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like