为了账号安全,请及时绑定邮箱和手机立即绑定

Windows批处理脚本读取.ini文件

Windows批处理脚本读取.ini文件

米脂 2019-11-30 14:56:11
我正在尝试读取.ini以下格式的文件:[SectionName]total=4[AnotherSectionName]total=7[OtherSectionName]total=12基本上,我想从.ini文件中打印出某些值,例如,总计不足,OtherSectionName然后是总计AnotherSectionName。
查看完整描述

3 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

ini.cmd您可以使用以下命令文件()提取相关值:


@setlocal enableextensions enabledelayedexpansion

@echo off

set file=%~1

set area=[%~2]

set key=%~3

set currarea=

for /f "usebackq delims=" %%a in ("!file!") do (

    set ln=%%a

    if "x!ln:~0,1!"=="x[" (

        set currarea=!ln!

    ) else (

        for /f "tokens=1,2 delims==" %%b in ("!ln!") do (

            set currkey=%%b

            set currval=%%c

            if "x!area!"=="x!currarea!" if "x!key!"=="x!currkey!" (

                echo !currval!

            )

        )

    )

)

endlocal

这是一个在运行中的成绩单记录(我已手动缩进输出内容以便于阅读):


c:\src>type ini.ini

    [SectionName]

    total=4

    [AnotherSectionName]

    total=7

    [OtherSectionName]

    total=12

c:\src>ini.cmd ini.ini SectionName total

    4

c:\src>ini.cmd ini.ini AnotherSectionName total

    7

c:\src>ini.cmd ini.ini OtherSectionName total

    12

要在另一个cmd文件中实际使用它,只需将echo %val%下面的行替换为您想对其执行的任何操作):


for /f "delims=" %%a in ('call ini.cmd ini.ini AnotherSectionName total') do (

    set val=%%a

)

echo %val%


查看完整回答
反对 回复 2019-11-30
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

我有一个简短的建议,可以从Windows批处理(.bat)的当前目录中读取config.ini文件:


在批处理文件的末尾附近,我们粘贴以下代码:


:ini    

@for /f "tokens=2 delims==" %%a in ('find "%~1=" config.ini') do @set %~2=%%a    

@goto:eof

在批处理文件开始附近,我们通过以下方式调用它:


@call:ini IniFieldName batchVarName

@echo IniFieldName is: %batchVarName%


查看完整回答
反对 回复 2019-11-30
  • 3 回答
  • 0 关注
  • 1851 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信