如何获取现有的环境变量,更改其值并在更改后使用 CMD 命令显示它?

如何获取现有的环境变量,更改其值并在更改后使用 CMD 命令显示它?

因此,在与 Berend 讨论后,我发现我需要更好地解释自己,这就是我要做的。因此,我的任务是编写一个 Windows 批处理脚本,它将:

  1. 展示(打印到屏幕)我拥有的环境变量中的任何值(但也许我需要检查它是否存在,如果不存在,我需要创建一个?我不确定)1.1 如果我需要检查变量是否存在,是否有任何 if 或 for 循环可以检查它?如果没有,我想我只需创建新的环境变量并使用它。

  2. 我需要更新刚刚打印的变量的值。

  3. 显示当前变量的值我刚刚在步骤2中所做的更新。

我希望现在比以前更清楚,我很乐意得到任何帮助。

答案1

从您的评论中我了解到您想要更改 PATH 变量中的一个目录。

例如,如果 PATH 是,C:\WINDOWS\system32;C:\WINDOWS;C:\MyFolder则需要更改为C:\WINDOWS\system32;C:\WINDOWS;C:\MyOtherFolder

您可以这样做:

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

语法是%VARIABLE:SEARCH=REPLACE%

答案2

检查变量是否存在非常简单:

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

检查变量是否具有空值:

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

查看简单变量(一个条目变量)是否具有特定值,如果没有,则将其更改为您想要的值:

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

一些内容如上所述,但没有永久性的改变:

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

要更改用户或系统 %path% 变量,这要稍微复杂一些,因为这个变量可以有多个以分号分隔的条目,下面是将用户 %path% 变量批量更改为新值的示例:

您需要做的是更改以下行:

设置旧条目=

设置新条目=

我批量添加了很多评论,以便您知道发生了什么。

在此视频中我展示了它的实际操作 YouTube

代码:

@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

答案3

要添加或更改变量中的数字:

@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

相关内容