当前位置:首页 > 行业动态 > 正文

批处理 set命令

批处理中的set命令用于设置环境变量。

批处理BAT脚本是Windows操作系统中一种非常实用的脚本语言,它可以帮助我们自动化完成一些重复性的工作,在批处理脚本中,set命令是一个非常重要的命令,它可以用来设置或显示环境变量,本文将对set命令的使用进行详细的介绍。

set命令的基本用法

1、设置环境变量

使用set命令可以设置环境变量,语法如下:

set 变量名=变量值

我们可以设置一个名为PATH的环境变量,用于存储程序的搜索路径:

set PATH=C:Windows;C:Program FilesJava;C:Program FilesPython

2、显示环境变量

使用set命令可以显示当前已经设置的所有环境变量,语法如下:

set

我们可以查看当前已经设置的所有环境变量:

set

set命令的常用选项

1、/a:追加环境变量,而不是覆盖原有的值,如果环境变量已经存在,则在其值的后面追加新的值;如果环境变量不存在,则直接设置新的值。

我们可以追加一个名为JAVA_HOME的环境变量:

set JAVA_HOME=C:Program FilesJava /a

2、/p:提示用户输入要设置的值,这对于需要用户输入的环境变量非常有用。

我们可以提示用户输入一个名为USERNAME的环境变量:

set /p USERNAME=请输入用户名:

3、/x:删除指定的环境变量,如果环境变量不存在,则不执行任何操作。

我们可以删除一个名为TEST的环境变量:

set TEST= /x

set命令的注意事项

1、set命令设置的环境变量仅对当前的批处理脚本有效,当脚本执行完毕后,设置的环境变量会自动失效,如果需要永久设置环境变量,可以将set命令添加到系统的环境变量配置文件中(如Windows系统中的system32driversetchosts文件)。

2、set命令设置的环境变量是以字符串的形式存储的,即使我们需要设置的是数字或其他类型的值,也需要将其转换为字符串,我们可以将一个数字转换为字符串并设置为环境变量:

set NUMBER=12345 /a
echo %NUMBER% > temp.txt && type temp.txt | findstr /r "^[0-9]*$" > nul && set /p NUMBER=<temp.txt && del temp.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMBER% >> num.txt && for /f "delims=" %%i in (num.txt) do set NUMBER=%%i && del num.txt && echo %NUMBER%:%NUMTALBREAK>12345:12345>> num.txt && for /f "delims=" %i in (num.txt) do set NUMBER=!i! & goto endnum & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch & endlocal & goto endbatch &endnum:&for /f "tokens=*" %j in ('dir /b C:') do if not "%~j" == "num.txt" if not "%~j" == "nul" if not "%~j" == "temp.txt" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0" if not "%~j" == "%~nx0" if not "%~j" == "%~dp0ifnot "%~j" == "%~nx0ifnot "%~j" == "%~dp0ifnot "%~j" == apendnumifnot ""==12345goto startnum&goto startnum&goto start
0

随机文章