读取 .ini 文件的 Windows 批处理脚本 [英] Windows batch script to read an .ini file
问题描述
我正在尝试使用以下格式读取 .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%
这篇关于读取 .ini 文件的 Windows 批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!