首页 > 代码库 > dos环境变量设置及字符串替换
dos环境变量设置及字符串替换
(1) 通过wmic命令完毕对系统环境变量的设定,xp下直接生效,win7须要重新启动电脑或在电脑-》属性-》高级-》环境变量,下来点确定
@echo off
set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
::echo.
::echo===============================================
::echo 安装设置
::echo===============================================
::echo.
::set /p en=请按 回车键 安装開始!:
::echo.
set pwd=%cd%
::reg add "%regpath%" /v "winrar" /d "%pwd%\compact;" /f
::echo.
::echo 新追加环境变量 path=%pwd%\WinRAR
:: for,/f,"skip=4 token=1,2,*",%%a,in,(‘reg query "%regpath%" /v "path"‘),do,(
:: reg add "%regpath%" /v "path" /d "%%c;%pwd%\compact;%pwd%\conf\bin;%pwd%\lib;" /f
::)
wmic ENVIRONMENT where "name=‘path‘ and username=‘<system>‘" set VariableValue="http://www.mamicode.com/%PATH%;%pwd%/compact;%pwd%/conf/bin;%pwd%/lib;"
wmic ENVIRONMENT create name="TESSDATA_PREFIX",username="<system>",VariableValue="http://www.mamicode.com/%pwd%/lib"
echo.
(2)字符串替换,注意要替换字符串里的比較复杂的符号,须要用!!,而该符呈的使用必须setlocal enabledelayedexpansion和endlocal来加标识
::以替换为空的方式删除Path的环境变量
set p=%PATH%
set pwd=%cd%
set replace=%pwd%\compact^;%pwd%\conf\bin^;%pwd%\lib^;
setlocal enabledelayedexpansion
set p=!p:%replace%=!
wmic ENVIRONMENT where "name=‘path‘ and username=‘<system>‘" set VariableValue="http://www.mamicode.com/%p%;"
endlocal
(3)正常情况的替换将“aabbcc”中的b串替换为空
set a="aabbcc"
set b=%a:b=%
echo %b%
dos环境变量设置及字符串替换