批处理读取INI文件

  Quote:

  1. @echo off
  2. :::::::::INI文件读取 by chenall QQ:366840202::::::::::::::::::::::
  3. ::使用方法:                                                        ::
  4. ::    inifile iniFilePath [section] [item]                        ::
  5. ::例子:                                                                ::
  6. ::        inifile c:\boot.ini                                        ::
  7. ::        读取c:\boot.ini的所有[section]                                ::
  8. ::        inifile c:\boot.ini "[boot loader]"                        ::
  9. ::        读取c:\boot.ini [boot loader]段的内容                        ::
  10. ::        inifile c:\boot.ini "[boot loader]" timeout                ::
  11. ::        显示c:\boot.ini [boot loader]段 timeout的值                ::
  12. ::                                                                ::
  13. ::::::::::::::::::::::::::::::::::::::::::::2006-12-18::::::::::::

  14. set item=
  15. set filepath=
  16. set section=
  17. setlocal EnableDelayedExpansion
  18. if not "%~1"=="" (
  19.         set filepath=%1
  20. ) else goto :file_err
  21. if not exist %filepath% goto :file_err
  22. if not "%~2"=="" (
  23.         set section=%2
  24.         if "!section:~0,1!"==""^" set section=!section:~1!
  25.         if "!section:~-1!"==""^" set section=!section:~0,-1!
  26. ) else goto :section
  27. if not "%~3"=="" (
  28.         set item=%3
  29.         if "!item:~0,1!"==""^" set item=!item:~1!
  30.         if "!item:~-1!"==""^" set item=!item:~0,-1!
  31. )
  32. setlocal disableDelayedExpansion
  33. set 字段开始=
  34. for /f "usebackq delims=[]" %%i in (`find /i "%section%" /n %filepath%`) do set 字段开始=%%i
  35. for /f "usebackq tokens=1* delims== skip=%字段开始%" %%i in (`type %filepath%`) do (
  36.         set a=%%i
  37.         setlocal EnableDelayedExpansion
  38.         if "!a:~0,1!"=="[" goto :eof
  39.         if not "!a:~0,1!"==";" (
  40.                 setlocal disableDelayedExpansion
  41.                 for /f "delims=;" %%x in ("%%i=%%j") do (
  42.                         if not DEFINED item (echo %%x) else (if /i "%%i"=="%item%" echo %%x)
  43.                 )
  44.         )
  45. )
  46. goto :eof

  47. :section
  48. setlocal disableDelayedExpansion
  49. for /f "usebackq delims== skip=2" %%i in (`find /i "[" %filepath%`) do echo %%i
  50. goto :eof

  51. :file_err
  52. setlocal disableDelayedExpansion
  53. echo.
  54. echo %1文件未找到或未输入!
  55. echo.
  56. goto :eof
        
分享到: 更多
原文地址:https://www.cnblogs.com/tiasys/p/1123519.html