中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-05 22:02
48,039 主题排行 / 350,124 发帖 / 今日 2 篇 / 48,251 会员排行
DOS批处理 & 脚本技术(批处理室) » 关于批处理读取INI文件
可打印版本  1,648 / 12
第1楼 rrrrrrr 发表于 2007-04-26 00:04
新手上路 发帖 6 积分 13
关于批处理读取INI文件
坛子以前有篇帖子介绍批处理读文件
http://www.cn-dos.net/forum/viewthread.php?tid=25789&sid=PMXBgq
功能已经很完善,我现在想把结果写进一个变量比如result
再循环内部用
set result = %%x
循环外部用echo %result%显示不出结果
总说echo处于关闭状态
请问如何解决这个问题。

代码如下:


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

:: ::

::::::::::::::::::::::::::::::::::::::::::::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!"=="













%%x&endlocal

)

endlocal

)

)

endlocal

endlocal

)

endlocal

goto :eof



:section

endlocal

for /f "eol=; usebackq delims== skip=2" %%i in (`find /i "



































第2楼 wudixin96 发表于 2007-04-26 01:01
银牌会员 发帖 931 积分 1,928
没见到result有什么用啊?
第3楼 lxmxn 发表于 2007-04-26 01:16
版主 发帖 4,938 积分 11,386
直接set result=%%i,然后在循环外面用echo %result%来引用其值。
第4楼 rrrrrrr 发表于 2007-04-26 03:48
新手上路 发帖 6 积分 13
Originally posted by lxmxn at 2007-4-25 12:16 PM:
直接set result=%%i,然后在循环外面用echo %result%来引用其值。


不行
循环外面用echo %result% 显示不出内容来
第5楼 lxmxn 发表于 2007-04-26 05:20
版主 发帖 4,938 积分 11,386
他这个批处理可能本身就有问题,我测试以后发现根本没有完全显示ini文件的内容,要另外修改才可使用。
第6楼 chenall 发表于 2007-04-26 11:37
银牌会员 发帖 469 积分 1,276 来自 福建泉州
因为批处理里面使用了setlocal设置的变量是无法在批处理外使用的.
目前只好使用for来读取设置变量了.
我之前没有注意以为只要修改一下就可以了.对不起了.
以后有空再改改那个批处理.

[ Last edited by chenall on 2007-4-26 at 11:38 AM ]
第7楼 chenall 发表于 2007-04-26 11:58
银牌会员 发帖 469 积分 1,276 来自 福建泉州
新版已经改过来了,可以设置变量.

楼主可以去试试新版本的,要设置变量之前请将里面的setvar=0修改为1
第8楼 rrrrrrr 发表于 2007-04-26 12:50
新手上路 发帖 6 积分 13
Originally posted by chenall at 2007-4-25 10:37 PM:
因为批处理里面使用了setlocal设置的变量是无法在批处理外使用的.
目前只好使用for来读取设置变量了.
我之前没有注意以为只要修改一下就可以了.对 ...


确实是setlocal出问题了,已经修改成功了。
谢谢您:P
第9楼 rrrrrrr 发表于 2007-04-26 13:12
新手上路 发帖 6 积分 13
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 ::
::例子: ::
:: 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即可 ::
::::::::::::::::::::::::::::::::::::::::::::2006-12-21::::::::::::
set setvar=1
::当有指定参娄并且setvar值为1时就将的值设为变量
::例子inifile c:\boot.ini "" timeout 就可以得到一个%timeout%的变量
set item=
set filepath=
set section=
call :inifile c:\boot.ini "" 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&set "item=%%j"

)
)
)
)

goto :eof

:section
endlocal
for /f "eol=; usebackq delims== skip=2" %%i in (`find /i "

















第10楼 chenall 发表于 2007-04-26 14:24
银牌会员 发帖 469 积分 1,276 来自 福建泉州
set "item=%%j"
应该改为
set "%%x"
因为在这里主要是要过滤;后面的内容.如果set "item=%%j"
那前面的for语句就没有作用了.
如果你根本就不需要这个过滤.也就是说没有注释符之类的的那前面过滤";"的for语句完全可以不要.
第11楼 rrrrrrr 发表于 2007-04-27 00:47
新手上路 发帖 6 积分 13
嗯 明白了

我不需要过滤
只要取出指定的item就好了
第12楼 stance 发表于 2008-05-04 11:11
初级用户 发帖 46 积分 64
Originally posted by chenall at 2007-4-26 02:24 PM:
set "item=%%j"
应该改为
set "%%x"
因为在这里主要是要过滤;后面的内容.如果set "item=%%j"
那前面的for语句就没有作用了.
如果你根 ...



這個我需要。
第13楼 knoppix7 发表于 2008-05-04 17:46
银牌会员 发帖 634 积分 1,287 来自 cmd.exe
随便写一个...
没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
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023