Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201275
  • 博文数量: 48
  • 博客积分: 1935
  • 博客等级: 上尉
  • 技术积分: 491
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 00:59
文章分类

全部博文(48)

文章存档

2011年(1)

2010年(47)

我的朋友

分类: WINDOWS

2010-07-29 01:04:47

@echo off
:::::::::INI文件读取::::::::::::::::::::::
::使用方法:       ::
::    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的值  ::
:: 新增设置变量的功能,只需将下面的setvar=0改为1即可 ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set setvar=0
::当有指定[item]参娄并且setvar值为1时就将[item]的值设为变量[item]
::例子inifile c:\boot.ini "[boot loader]" timeout 就可以得到一个%timeout%的变量
set item=
set filepath=
set section=
if not "%~1"=="" (
 set filepath=%1
) else goto :file_err
if not exist %filepath% goto :file_err
setlocal EnableDelayedExpansion
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!
)
endlocal&set "item=%item%"&set "section=%section%"
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 (
 set a=%%i
 setlocal EnableDelayedExpansion
 if "!a:~0,1!"=="[" (endlocal&goto :eof)
 endlocal
 for /f "delims=;" %%x in ("%%i=%%j") do (
  if not DEFINED item (echo %%x) else (
   setlocal EnableDelayedExpansion
   call :trim a
   if /i "!a!"=="%item%" (
    if "%setvar%"=="1" (
     endlocal&set "%%x"
    ) else (
     endlocal&echo %%x
    )
   )
  )
 )
)
goto :eof
:section
endlocal
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
:show_item
if not DEFINED item (echo %b%) else (if /i "%a%"=="%item%" echo %%x)
goto :eof
:file_err
echo.
echo %1文件未找到或未输入!
echo.
goto :eof
 
阅读(3479) | 评论(3) | 转发(0) |
0

上一篇:没有了

下一篇:qemu [How to use Network]

给主人留下些什么吧!~~

chinaunix网友2010-07-29 01:21:49

D:\>@echo off && for /f "tokens=1-2 delims==" %a in (%abc%) do set one=%a && set two=%b && @echo on D:\>echo %one% timeout D:\>echo %two% 100

chinaunix网友2010-07-29 01:15:33

D:\>set abc="timeout=100" D:\>for /f "tokens=2 delims==" %a in (%abc%) do echo %a

chinaunix网友2010-07-29 01:10:27

D:\>@echo off && for /f "tokens=1-5 delims=./ " %a in (a.txt) do echo %a %b %c % d %e && @echo on 111222 333 444 555 D:\>type a.txt 111222.333 444/555