从Windows批处理文件读取INI [英] Read ini from windows batch file

查看:430
本文介绍了从Windows批处理文件读取INI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取该格式的ini文件:

I'm trying to read a ini file with this format:

[SectionName]
total=4
[AnotherSectionName]
total=7
[OtherSectionName]
total=12

基本上我想呼应了从INI文件(某些值如:在 OtherSectionName 总其次是共有来自 AnotherSectionName )。

推荐答案

下面是一个命令文件( ini.cmd ),你可以用它来提取相关的值:

Here's a command file (ini.cmd) you can use to extract the relevant values:

@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

和这里的成绩单显示它在行动(我手动缩进输出,使其更易于阅读):

And here's a transcript showing it in action (I've manually indented the output to make it easier to read):

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 文件实际上使用,只需更换回声%VAL%线下不管你想用它做):

To actually use this in another cmd file, just replace the echo %val% line below with whatever you want to do with it):

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

这篇关于从Windows批处理文件读取INI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆