问题描述
我正在尝试读取具有以下格式的.ini
文件:
I'm trying to read an .ini
file with the following format:
[SectionName]
total=4
[AnotherSectionName]
total=7
[OtherSectionName]
total=12
基本上我想从.ini
文件中打印出某些值,例如OtherSectionName
下的总数,然后是AnotherSectionName
中的总数.
Basically I want to print out certain values from the .ini
file, for example the total under OtherSectionName
followed by the total from 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
文件中实际使用此功能,只需将下面的echo %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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!