【问题标题】:Could batch file recognize the fields in the cfg file?批处理文件可以识别cfg文件中的字段吗?
【发布时间】:2015-04-09 09:31:18
【问题描述】:

我有一个包含两个字段的 cfg 文件:test.cfg

[Test_1] = 12345
TC_1=testCase/String_print.tc -arg "Indonesia" -logToConsole
TC_2=testCase/test2.tc -c "ramen" -logToConsole

[Test_2] = 12346
TC_1= testCase/test.tc -c "olleH" -logToConsole

和bat文件test.bat

@ECHO off

set var=C:\Users\syuniyar\.EasyTest\4\ccl\config\test2.cfg
SETLOCAL enabledelayedexpansion

FOR /F "tokens=*" %%a IN (%var%) DO (
    SET line=%%a
    IF "!line:~0,7!"=="Test_1=" SET Test_1=!line:~7! (
            FOR /F "tokens=*" %%a IN (%var%) DO (
                IF "!line:~0,5!"=="TC_1=" SET TC_1=!line:~5!
                IF "!line:~0,5!"=="TC_2=" SET TC_2=!line:~5!
)
)
)
SET TC_1=%TC_1:;=%
SET TC_2=%TC_2:;=%

ECHO %TC_1%
ECHO %TC_2%

我想检索 Test_1 = 12345 中的 TC_1,它的值为 'testCase/String_print.tc -arg "Indonesia" -logToConsole'。但是从当前代码中,我得到的是来自 12346 的 'testCase/test.tc -c "olleH" -logToConsole'。它返回匹配的最后一个项目。我的问题是:批处理文件是否有可能识别 Test_1\TC_1 之类的东西?在我尝试使用数组之前,它会将 cfg 文件中的所有元素存储到一个数组中。

有人可以帮忙吗?

【问题讨论】:

  • 您的描述不清楚。鉴于上面的 test.cfg 文件和 which input 你想要的 output 是什么?请添加此信息以编辑问题,并在此处给我留言作为建议。
  • 感谢您的建议。我改变了一点。抱歉不清楚。

标签: batch-file configuration config


【解决方案1】:

此批处理文件执行您的要求:

@echo off
setlocal EnableDelayedExpansion

rem Get the line number of "[Test_1]"
for /F "delims=:" %%a in ('findstr /N /L /B "[Test_1]" test2.cfg') do set lines=%%a

rem Get the TC_1 line after previous one
for /F "skip=%lines% tokens=1,2 delims==" %%a in (test2.cfg) do (
   if "%%a" equ "TC_1" set "%%a=%%b" & goto continue
)
:continue

echo TC_1=%TC_1%

【讨论】:

    猜你喜欢
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多