:::::::::INI文件读取 by chenall QQ:366840202::::::::::::::::::::::
::使用方法:
:: inifile iniFilePath
::例子:
:: inifile c:\boot.ini
:: 读取c:\boot.ini的所有
:: inifile c:\boot.ini ""
:: 读取c:\boot.ini 段的内容
:: inifile c:\boot.ini "" timeout
:: 显示c:\boot.ini 段 timeout的值
::
::::::::::::::::::::::::::::::::::::::::::::2006-12-18::::::::::::
因自己的需要编写的一个批处理,大家可共同完善.
[ Last edited by chenall on 2009-11-6 at 19:44 ]
::使用方法:
:: inifile iniFilePath
::例子:
:: inifile c:\boot.ini
:: 读取c:\boot.ini的所有
:: inifile c:\boot.ini ""
:: 读取c:\boot.ini 段的内容
:: inifile c:\boot.ini "" timeout
:: 显示c:\boot.ini 段 timeout的值
::
::::::::::::::::::::::::::::::::::::::::::::2006-12-18::::::::::::
因自己的需要编写的一个批处理,大家可共同完善.
chenall 发表于: 2009-11-06 19:17
- @echo off
- :::::::::INI文件读取 by chenall QQ:366840202::::::::::::::::::::::
- ::使用方法: ::
- :: inifile iniFilePath ::
- ::例子: ::
- :: inifile c:\boot.ini ::
- :: 读取c:\boot.ini的所有 ::
- :: inifile c:\boot.ini "" ::
- :: 读取c:\boot.ini 段的内容 ::
- :: inifile c:\boot.ini "" timeout ::
- :: 显示c:\boot.ini 段 timeout的值 ::
- ::07-04-26 新增设置变量的功能,只需将下面的setvar=0改为1即可 ::
- ::09-11-05 重写并优化部份代码,修正不能正确设置变量的BUG ::
- :: 100:文件不存在或未找到 ::
- ::::::::::::::::::::::::::::::::::::::::::::2006-12-21::::::::::::
- SETLOCAL
- set setvar=1
- ::当有指定参娄并且setvar值为1时就将的值设为变量
- ::例子inifile c:\boot.ini "" 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%"
- ::如果获取到的第一个字符是"
- 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
[ Last edited by chenall on 2009-11-6 at 19:44 ]
本帖最近评分记录
(共 3 条)
点击查看详情
| 评分人 | 分数 | 时间 |
|---|---|---|
| electronixtar | +11 | 2007-03-14 02:40 |
| yangzhiyi | +2 | 2007-03-17 08:11 |
| 0401 | +4 | 2007-03-17 09:27 |
附件
QQ:366840202
http://chenall.net
http://chenall.net
