[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统增强] 批处理删除path变量中无效的路径并整合重复的路径

测试环境: WinXP SP3 CHS
  1. @echo off & setlocal enableDelayedExpansion
  2. :remself 合并环境变量 path 中 重复的路径值, 并删除不存在的路径
  3. REM 移除 path 值中可能含有的 控制字符 CR (回车但不换行)
  4. for /f "delims=" %%a in ("!path!") do set pth=%%~a
  5. REM 原始 PATH 路径项存到 $path 数组
  6. call :delTailSP pth
  7. set pth="!pth!"
  8. REM 加入问号避免出现连续双引号
  9. set pth=!pth:;=" "?!
  10. set pth=!pth:""="!
  11. for %%a in (!pth:?^=!) do (
  12.   set tt=%%~a
  13.   call :delTailSlash tt
  14.   REM attrib -r -s -h "!tt!"
  15.   if exist "!tt!\" set "$path!tt!=!tt!"
  16. )
  17. set $path
  18. REM 在 $path 数组中删去 系统 基本路径项
  19. for %%a in ("%windir%" "%windir%\system32" "%windir%\system32\wbem" "%windir%\system32\dllcache") do set "$path%%~a="
  20. REM 在 path 中添加 系统 基本路径项
  21. cd /d "%windir%\system32\wbem\"
  22. set sysPath=%windir%;%windir%\system32;%windir%\system32\wbem;%windir%\system32\dllcache
  23. wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" set VariableValue='!sysPath!'
  24. for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do echo %%a
  25. REM 把 $path 数组中所有项目添加到 path 变量中
  26. for /f "tokens=2 delims==" %%p in ('set $path') do (
  27.   for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do (
  28.     wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" set VariableValue='%%a;%%~p'
  29.   )
  30. )
  31. REM 去除多余空格
  32. for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do (
  33.   echo. & echo 去除多余空格之前, path 的值& echo %%a
  34.   set "thePath=%%a"
  35.   wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" set VariableValue='!thePath:  ;=;!'
  36. )
  37. echo. & echo 去除多余空格后, path 的值
  38. for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "name='path' and username='<SYSTEM>'" get VariableValue') do echo %%a
  39. pause
  40. exit /b
  41. REM 删除字符串尾部的若干空格
  42. :delTailSP str
  43. if "!%1:~-1!" leq " " (
  44.   set %1=!%1:~0,-1!
  45.   call :delTailSP %1
  46. )
  47. exit /b
  48. REM 删除字符串尾部的若干反斜线
  49. :delTailSlash str
  50. if "!%1:~-1!"=="\" (
  51.   set %1=!%1:~0,-1!
  52.   call :delTailSP %1
  53. )
  54. exit /b
复制代码

楼主的大作都是非常实用的!收藏了!
能备份原%path%就更好了!
有一种爱叫放弃

TOP

返回列表