@echo off
:::::::::INI文件读取 by chenall QQ:366840202::::::::::::::::::::::
::使用方法: ::
:: inifile iniFilePath [section] [item] ::
::例子: ::
:: inifile c:\boot.ini ::
:: 读取c:\boot.ini的所有[section] ::
:: inifile c:\boot.ini "[boot loader]" ::
:: 读取c:\boot.ini [boot loader]段的内容 ::
:: inifile c:\boot.ini "[boot loader]" timeout ::
:: 显示c:\boot.ini [boot loader]段 timeout的值 ::
::07-04-26 新增设置变量的功能,只需将下面的setvar=0改为1即可 ::
::09-11-05 重写并优化部份代码,修正不能正确设置变量的BUG ::
:: 100:文件不存在或未找到 ::
::::::::::::::::::::::::::::::::::::::::::::2006-12-21::::::::::::
SETLOCAL
set setvar=1
::当有指定[item]参娄并且setvar值为1时就将[item]的值设为变量[item]
::例子inifile c:\boot.ini "[boot loader]" timeout 就可以得到一个%timeout%的变量
::初始化变量
set exit_code=
set item=
set item_value=inifile=没有找到指定项目!
set filepath=
set section=
set inifile=
if not "%~1"=="" (
set filepath=%1
) else goto :file_err
if not exist %filepath% goto :file_err
if "%~2"=="" goto :section
set "section=%~2"
set "item=%~3"
call :开始
endlocal&if defined item set %item_value%
goto :eof
:开始
for /f "usebackq delims=[] skip=2" %%i in (`find /i "%section%" /n %filepath%`) do set 字段开始=%%i
if "%字段开始%"=="" goto :eof
for /f "eol=; usebackq tokens=1* skip=%字段开始% delims==" %%i in (`type %filepath%`) do (
call :分析数据 "%%i" "%%j"
::强制退出,并返回一个退出代码
if defined exit_code exit /b %exit_code%
)
goto :eof
:分析数据
set "a=%~1"
setlocal EnableDelayedExpansion
call :trim a
endlocal & set "a=%a%"
::如果获取到的第一个字符是"[",说明本节已经搜索完成。退出,返回0
if "%a:~0,1%"=="[" (set exit_code=0&goto :eof)
::分析数据,因为INI文件一般“;”以后是注释符,这里去掉注释。
for /f "delims=;" %%x in ("%a%=%~2") do (
if not DEFINED item (echo %%x) else (
if /i "%a%"=="%item%" (
set exit_code=0
if "%setvar%"=="1" (
set "item_value=%%x"
) else (
echo %%x
)
)
)
)
goto :eof
:section
for /f "eol=; usebackq delims== skip=2" %%i in (`find /i "[" %filepath%`) do echo %%i
goto :eof
:trim
if "!%1:~0,1!"==" " (set %1=!%1:~1!&&goto trim)
if "!%1:~0,1!"==" " (set %1=!%1:~1!&&goto trim)
if "!%1:~-1!"==" " (set %1=!%1:~0,-1!&&goto trim)
if "!%1:~-1!"==" " (set %1=!%1:~0,-1!&&goto trim)
goto :eof
:file_err
echo.
echo %1文件未找到或未输入!
echo.
exit /b 100
goto :eof
inifile iniFilePath [section] [item]
@echo off
set item=
set filepath=
set section=
setlocal EnableDelayedExpansion
if not "%~1"=="" (
set filepath=%1
) else goto :file_err
if not exist %filepath% goto :file_err
if not "%~2"=="" (
set section=%2
if "!section:~0,1!"==""^" set section=!section:~1!
if "!section:~-1!"==""^" set section=!section:~0,-1!
) else goto :section
if not "%~3"=="" (
set item=%3
if "!item:~0,1!"==""^" set item=!item:~1!
if "!item:~-1!"==""^" set item=!item:~0,-1!
)
setlocal disableDelayedExpansion
set 字段开始=
for /f "usebackq delims=[]" %%i in (`find /i "%section%" /n %filepath%`) do set 字段开始=%%i
for /f "usebackq tokens=1* delims== skip=%字段开始%" %%i in (`type %filepath%`) do (
set a=%%i
setlocal EnableDelayedExpansion
if "!a:~0,1!"=="[" goto :eof
if not "!a:~0,1!"==";" (
setlocal disableDelayedExpansion
for /f "delims=;" %%x in ("%%i=%%j") do (
if not DEFINED item (echo %%x) else (if /i "%%i"=="%item%" echo %%x)
)
)
)
goto :eof
:section
setlocal disableDelayedExpansion
for /f "usebackq delims== skip=2" %%i in (`find /i "[" %filepath%`) do echo %%i
goto :eof
:file_err
setlocal disableDelayedExpansion
echo.
echo %1文件未找到或未输入!
echo.
goto :eof
全部 @echo off for /f "tokens=2 delims==" %%i in (a.ini) do echo %%i 不一定全部,如果只要version值不要section把倒数第三行中间的echo !s! %%j改成 echo %%j @echo off setlocal enabledelayedexpansion set v=soft01 soft03 for %%a in (%v%) do ( for /f "tokens=1* delims=[]=" %%i in (a.ini) do ( if defined s echo !s! %%j&set s= if "%%i"=="%%a" set s=%%i)) pause
-
http://bbs.csdn.net/topics/230030667
-
已经能够实现我的目的,还想再请教下: 已知section可能会是soft01、soft02、……、soft20(亦即范围是已知的) 但是在这个ini文件中不一定会全部存在,只要某个section存在就显示出version值,是不是第三行应该改为(ini文件中同时还会存在其它无用的section,那些不用显示): set v=soft01 soft02 soft03 …… soft20