我有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&#39 ;.但是从目前的代码来看,我得到的是' testCase / test.tc -c" olleH" -logToConsole'它来自12346.它返回匹配的最后一项。我的问题是:批处理文件是否可以识别Test_1 \ TC_1之类的内容?在我尝试使用数组之前,它将cfg文件中的所有元素存储到一个单独的数组中。
有人可以帮忙吗?
答案 0 :(得分:0)
此批处理文件按您的要求执行:
@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%