我目前正在尝试循环遍历 a 中的每个项目.ini
文件并稍后使用这些值。但我不知道怎么做。我的config.ini
文件看起来像这样:
[items]
item_1=XXXXX
item_2=XXXXX
item_3=XXXXX
item_4=XXXXX
[SomeSection]
......
我找到了一种迭代和回显每个项目的方法config.ini
文件,像这样:
@echo off
for /F %%i in (config.ini) do (
echo %%i
)
我的问题是我想使用特定的值。所以我必须检查类别和键config.ini
文件。我尝试使用这个,但遇到了错误:
@echo off
for /F %%i in (config.ini) do (
SET item = %%i
if %item%==[items] (
rem do something here with the key and values now
)
)
正如我已经提到的,我无法将这些值保存到另一个变量,这导致我无法使用它们。
一个很简单的做法是预先确定目标节头的行号,然后在读取配置文件时跳过很多行,一旦出现另一个括号内的字符串就停止:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_CONFIG=%~dp0config.ini" & rem // (path to configuration file)
set "_SECT=items" & rem // (section name without brackets)
rem // Clean up variables whose names begin with `$`:
for /F "delims==" %%V in ('2^> nul set "$"') do set "%%V="
rem // Gather number of line containing the given section (ignoring case):
for /F "delims=:" %%N in ('findstr /N /I /X /C:"[%_SECT%]" "%_CONFIG%"') do set "SKIP=%%N"
rem // Read configuration file, skipping everything up to the section header:
for /F "usebackq skip=%SKIP% delims=" %%I in ("%_CONFIG%") do (
rem // Leave loop as soon as another section header is reached:
for /F "tokens=1* delims=[]" %%K in ("%%I") do if "[%%K]%%L"=="%%I" goto :NEXT
rem // Do something with the key/value pair, like echoing it:
echo(%%I
rem // Assign a variable named of `$` + key and assign the value:
set "$%%I"
)
:NEXT
rem // Return assigned variables:
set "$"
endlocal
exit /B
该脚本将根据您的示例配置文件分配以下变量:
$item_1=XXXXX
$item_2=XXXXX
$item_3=XXXXX
$item_4=XXXXX
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)