我正在尝试查找并使用特定单词作为变量,以使用批处理脚本打印到 txt 文件中。
这是文本示例。
10-03-2021 18:46:12:77 INFO: [SET_APP_VARS] Country:UK
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Langauge:ENGLISH
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Type:CLASSIC
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Lane:801
所以我需要找到单词CLASSIC
并将其用作变量
下面的代码正确吗?
FOR /F "skip=2 tokens=1,2,3,4,5,6 delims=:" %%a IN (test.txt) do echo Country is "%%f" & goto next
:next
FOR /F "skip=4 tokens=1,2,3,4,5,6 delims=:" %%a IN (test.txt) do echo Motherboard is is "%%f" & goto done
:done
最佳答案
过滤您的输入有助于:
for /f "tokens=6 delims=:" %%a in ('find " Country:" test.txt') do echo Country is %%a
for /f "tokens=6 delims=:" %%a in ('find " Type:" test.txt') do echo Type is %%a
或者如果您需要更多:
for /f "tokens=8,9 delims=: " %%a in (test.txt) do echo %%a is %%b
或使用变量:
for /f "tokens=8,9 delims=: " %%a in (test.txt) do set "_%%a=%%b"
set _
或只是一些(比第一个片段快得多,因为它只需读取 test.txt
一次):
for /f "tokens=8,9 delims=: " %%a in ('findstr "Country: Type:" test.txt') do set "_%%a=%%b"
set _
关于batch-file - FOR/F 这是在批处理文件中跳过行的正确方法吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66582307/