中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 搜索 | 上传 | 帮助 »
作者:
标题: 关于批处理读取INI文件 上一主题 | 下一主题
rrrrrrr
新手上路




积分 13
发帖 6
注册 2007-4-25
状态 离线
『楼 主』:  关于批处理读取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 "





































2007-4-26 00:04
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
wudixin96
银牌会员





积分 1928
发帖 931
注册 2007-1-6
状态 离线
『第 2 楼』:  

没见到result有什么用啊?

2007-4-26 01:01
查看资料  发短消息  网志   编辑帖子  回复  引用回复
lxmxn
版主




积分 11386
发帖 4938
注册 2006-7-23
状态 离线
『第 3 楼』:  

直接set result=%%i,然后在循环外面用echo %result%来引用其值。

2007-4-26 01:16
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
rrrrrrr
新手上路




积分 13
发帖 6
注册 2007-4-25
状态 离线
『第 4 楼』:  

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


不行
循环外面用echo %result% 显示不出内容来

2007-4-26 03:48
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
lxmxn
版主




积分 11386
发帖 4938
注册 2006-7-23
状态 离线
『第 5 楼』:  

他这个批处理可能本身就有问题,我测试以后发现根本没有完全显示ini文件的内容,要另外修改才可使用。

2007-4-26 05:20
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
chenall
银牌会员




积分 1276
发帖 469
注册 2002-12-23
来自 福建泉州
状态 离线
『第 6 楼』:  

因为批处理里面使用了setlocal设置的变量是无法在批处理外使用的.
目前只好使用for来读取设置变量了.
我之前没有注意以为只要修改一下就可以了.对不起了.
以后有空再改改那个批处理.

Last edited by chenall on 2007-4-26 at 11:38 AM ]



QQ:366840202
http://chenall.net
2007-4-26 11:37
查看资料  发送邮件  访问主页  发短消息  网志  OICQ (366840202)  编辑帖子  回复  引用回复
chenall
银牌会员




积分 1276
发帖 469
注册 2002-12-23
来自 福建泉州
状态 离线
『第 7 楼』:  

新版已经改过来了,可以设置变量.

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



QQ:366840202
http://chenall.net
2007-4-26 11:58
查看资料  发送邮件  访问主页  发短消息  网志  OICQ (366840202)  编辑帖子  回复  引用回复
rrrrrrr
新手上路




积分 13
发帖 6
注册 2007-4-25
状态 离线
『第 8 楼』:  

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


确实是setlocal出问题了,已经修改成功了。
谢谢您:P

2007-4-26 12:50
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
rrrrrrr
新手上路




积分 13
发帖 6
注册 2007-4-25
状态 离线
『第 9 楼』:  

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 "



















2007-4-26 13:12
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
chenall
银牌会员




积分 1276
发帖 469
注册 2002-12-23
来自 福建泉州
状态 离线
『第 10 楼』:  

set "item=%%j"
应该改为
set "%%x"
因为在这里主要是要过滤;后面的内容.如果set "item=%%j"
那前面的for语句就没有作用了.
如果你根本就不需要这个过滤.也就是说没有注释符之类的的那前面过滤";"的for语句完全可以不要.



QQ:366840202
http://chenall.net
2007-4-26 14:24
查看资料  发送邮件  访问主页  发短消息  网志  OICQ (366840202)  编辑帖子  回复  引用回复
rrrrrrr
新手上路




积分 13
发帖 6
注册 2007-4-25
状态 离线
『第 11 楼』:  

嗯 明白了

我不需要过滤
只要取出指定的item就好了

2007-4-27 00:47
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
stance
初级用户





积分 64
发帖 46
注册 2008-4-21
状态 离线
『第 12 楼』:  

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



這個我需要。

2008-5-4 11:11
查看资料  发送邮件  发短消息  网志   编辑帖子  回复  引用回复
knoppix7
银牌会员





积分 1287
发帖 634
注册 2007-5-2
来自 cmd.exe
状态 离线
『第 13 楼』:  

随便写一个...
没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

2008-5-4 17:46
查看资料  发短消息  网志   编辑帖子  回复  引用回复

请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: