Board logo

标题: 关于批处理读取INI文件 [打印本页]

作者: rrrrrrr     时间: 2007-4-26 00:04    标题: 关于批处理读取INI文件

坛子以前有篇帖子介绍批处理读文件
http://www.cn-dos.net/forum/view ... 5789&sid=PMXBgq
功能已经很完善,我现在想把结果写进一个变量比如result
再循环内部用
set result = %%x
循环外部用echo %result%显示不出结果
总说echo处于关闭状态
请问如何解决这个问题。

代码如下:


@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的值                ::

::                                                                ::

::::::::::::::::::::::::::::::::::::::::::::2006-12-21::::::::::::


set result=
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!

)

setlocal disableDelayedExpansion

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

        call :trim a

        if "!a:~0,1!"=="[" (endlocal&goto :eof)

        setlocal disableDelayedExpansion

        for /f "delims=;" %%x in ("%%i=%%j") do (

                if not DEFINED item (echo %%x) else (

                        setlocal EnableDelayedExpansion

                        if /i "!a!"=="%item%" (

                                setlocal disableDelayedExpansion

                                echo %%x&endlocal

                        )

                        endlocal

                )

        )

        endlocal

        endlocal

)

endlocal

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
作者: wudixin96     时间: 2007-4-26 01:01
没见到result有什么用啊?
作者: lxmxn     时间: 2007-4-26 01:16
直接set result=%%i,然后在循环外面用echo %result%来引用其值。
作者: rrrrrrr     时间: 2007-4-26 03:48


  Quote:
Originally posted by lxmxn at 2007-4-25 12:16 PM:
直接set result=%%i,然后在循环外面用echo %result%来引用其值。

不行
循环外面用echo %result% 显示不出内容来
作者: lxmxn     时间: 2007-4-26 05:20
他这个批处理可能本身就有问题,我测试以后发现根本没有完全显示ini文件的内容,要另外修改才可使用。
作者: chenall     时间: 2007-4-26 11:37
因为批处理里面使用了setlocal设置的变量是无法在批处理外使用的.
目前只好使用for来读取设置变量了.
我之前没有注意以为只要修改一下就可以了.对不起了.
以后有空再改改那个批处理.

[ Last edited by chenall on 2007-4-26 at 11:38 AM ]
作者: chenall     时间: 2007-4-26 11:58
新版已经改过来了,可以设置变量.

楼主可以去试试新版本的,要设置变量之前请将里面的setvar=0修改为1
作者: rrrrrrr     时间: 2007-4-26 12:50


  Quote:
Originally posted by chenall at 2007-4-25 10:37 PM:
因为批处理里面使用了setlocal设置的变量是无法在批处理外使用的.
目前只好使用for来读取设置变量了.
我之前没有注意以为只要修改一下就可以了.对 ...

确实是setlocal出问题了,已经修改成功了。
谢谢您:P
作者: rrrrrrr     时间: 2007-4-26 13:12


  Quote:
Originally posted by chenall at 2007-4-25 10:58 PM:
新版已经改过来了,可以设置变量.

楼主可以去试试新版本的,要设置变量之前请将里面的setvar=0修改为1

其实我是想要这种效果,把读ini作为子函数。
主函数从ini取出一些需要的变量用作参数去做其他操作。
还好问题已经解决,非常感谢你的指点,再次感谢。


@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即可        ::
::::::::::::::::::::::::::::::::::::::::::::2006-12-21::::::::::::
set setvar=1
::当有指定[item]参娄并且setvar值为1时就将[item]的值设为变量[item]
::例子inifile c:\boot.ini "[boot loader]" timeout 就可以得到一个%timeout%的变量
set item=
set filepath=
set section=
call :inifile c:\boot.ini "[boot loader]" timeout
echo %item%

goto :eof


:inifile
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%" (

                        endlocal&set "item=%%j"
                               
                        )
                )
        )
)

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
作者: chenall     时间: 2007-4-26 14:24
set "item=%%j"
应该改为
set "%%x"
因为在这里主要是要过滤;后面的内容.如果set "item=%%j"
那前面的for语句就没有作用了.
如果你根本就不需要这个过滤.也就是说没有注释符之类的的那前面过滤";"的for语句完全可以不要.
作者: rrrrrrr     时间: 2007-4-27 00:47
嗯 明白了

我不需要过滤
只要取出指定的item就好了
作者: stance     时间: 2008-5-4 11:11


  Quote:
Originally posted by chenall at 2007-4-26 02:24 PM:
set "item=%%j"
应该改为
set "%%x"
因为在这里主要是要过滤;后面的内容.如果set "item=%%j"
那前面的for语句就没有作用了.
如果你根 ...

這個我需要。
作者: knoppix7     时间: 2008-5-4 17:46
随便写一个...
没LZ的好.就是短点..
@echo off
::%1 =>INI文件路径
::%2 =>段名 不用加[]
::%3 =>变量名
::%3为空则读出整段内容
::Command =>模式(如 SET/ECHO) {set:设置为变量 echo:显示 },不能为空
::YH =>设置是否使用引号YH="则加引号 YH不存在则不加
::作为子BAT调用的时候变量延迟必须在父BAT中被打开
::作为block调用的时候可以自己看着开
if NOT EXIST "%~1" (Echo File Not Found&GOTO :EOF)
if "%~2"==""  GOTO :EOF
for /F "eol=; tokens=1 delims=:" %%i IN ('findstr /I /n /c:"[%~2]" "%~1" ^| Findstr /v ";" ') DO (
        SET line=%%i
)
if "%line%"=="" (echo Section Not Found&GOTO :EOF)
FOR /f "eol=; tokens=1* skip=%line% delims=" %%i IN ('type "%~1"') DO (
        set "temp_1=%%i"
        if "!temp_1:~0,1!"=="[" (GOTO :EOF) ELSE (
                IF "%~3"=="" (%command% %YH%%%i%YH%) ELSE (
                        FOR /F "tokens=1 delims==" %%a IN (%YH%%%i%YH%) DO (
                                if /i "%%a"=="%~3" (%command% %YH%%%i%YH%)
                        )
                )
        )
)

set temp_1=


测试代码:存为BAT
@echo off&setlocal ENABLEDELAYEDEXPANSION
set YH="
set command=set
echo 设置变量
call :inifile "%~0" 设置变量
set words
pause
echo 显示内容
set YH=
set command=echo
call :inifile "%~0" 显示内容
pause
echo 输出文件
set YH=
set "command=>>test.bat echo"
call :inifile "%~0" 输出文件
call test.bat
pause
del test.bat
GOTO :EOF
:inifile
if NOT EXIST "%~1" (Echo File Not Found&GOTO :EOF)
if "%~2"==""  GOTO :EOF
for /F "eol=; tokens=1 delims=:" %%i IN ('findstr /I /n /c:"[%~2]" "%~1" ^| Findstr /v ";" ') DO (
        SET line=%%i
)
if "%line%"=="" (echo Section Not Found&GOTO :EOF)
FOR /f "eol=; tokens=1* skip=%line% delims=" %%i IN ('type "%~1"') DO (
        set "temp_1=%%i"
        if "!temp_1:~0,1!"=="[" (GOTO :EOF) ELSE (
                IF "%~3"=="" (%command% %YH%%%i%YH%) ELSE (
                        FOR /F "tokens=1 delims==" %%a IN (%YH%%%i%YH%) DO (
                                if /i "%%a"=="%~3" (%command% %YH%%%i%YH%)
                        )
                )
        )
)

set temp_1=
GOTO :EOF
::===========================================
[设置变量]
words=welcome to cn-dos
[显示内容]
welcome to cn-dos
[输出文件]
@echo off
echo output as "%~f0"
echo welcome to cn-dos
pause