标题: [系统增强] 批处理删除path变量中无效的路径并整合重复的路径 [打印本页]
作者: neorobin 时间: 2010-7-29 21:00 标题: 批处理删除path变量中无效的路径并整合重复的路径
测试环境: WinXP SP3 CHS- @echo off & setlocal enableDelayedExpansion
- :remself 合并环境变量 path 中 重复的路径值, 并删除不存在的路径
-
- REM 移除 path 值中可能含有的 控制字符 CR (回车但不换行)
- for /f "delims=" %%a in ("!path!") do set pth=%%~a
-
- REM 原始 PATH 路径项存到 $path 数组
- call :delTailSP pth
- set pth="!pth!"
- REM 加入问号避免出现连续双引号
- set pth=!pth:;=" "?!
- set pth=!pth:""="!
-
- for %%a in (!pth:?^=!) do (
- set tt=%%~a
- call :delTailSlash tt
- REM attrib -r -s -h "!tt!"
- if exist "!tt!\" set "$path!tt!=!tt!"
- )
- set $path
-
- REM 在 $path 数组中删去 系统 基本路径项
- for %%a in ("%windir%" "%windir%\system32" "%windir%\system32\wbem" "%windir%\system32\dllcache") do set "$path%%~a="
-
- REM 在 path 中添加 系统 基本路径项
- cd /d "%windir%\system32\wbem\"
- set sysPath=%windir%;%windir%\system32;%windir%\system32\wbem;%windir%\system32\dllcache
- wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" set VariableValue='!sysPath!'
- for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do echo %%a
-
- REM 把 $path 数组中所有项目添加到 path 变量中
- for /f "tokens=2 delims==" %%p in ('set $path') do (
- for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do (
- wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" set VariableValue='%%a;%%~p'
- )
- )
-
- REM 去除多余空格
- for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do (
- echo. & echo 去除多余空格之前, path 的值& echo %%a
- set "thePath=%%a"
- wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" set VariableValue='!thePath: ;=;!'
- )
- echo. & echo 去除多余空格后, path 的值
- for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do echo %%a
- pause
- exit /b
-
- REM 删除字符串尾部的若干空格
- :delTailSP str
- if "!%1:~-1!" leq " " (
- set %1=!%1:~0,-1!
- call :delTailSP %1
- )
- exit /b
-
- REM 删除字符串尾部的若干反斜线
- :delTailSlash str
- if "!%1:~-1!"=="\" (
- set %1=!%1:~0,-1!
- call :delTailSP %1
- )
- exit /b
复制代码
作者: zjw767676 时间: 2010-7-30 13:47
楼主的大作都是非常实用的!收藏了!
能备份原%path%就更好了!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |