FOR /F 这是在批处理文件中跳过一行的正确方法吗
FOR /F is this the correct method for skipping a line in a batch file
我正在尝试查找并使用特定单词作为变量,以使用批处理脚本打印到 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 _
根据我认为您可能正在做的事情,我可能会提供不同的方法:
@Echo Off
SetLocal EnableExtensions
For /F "UseBackQ Tokens=1,* Delims=]" %%G In ("test.txt") Do (
For /F "Tokens=1,* Delims=: " %%I In ("%%H") Do Echo Set "%%I=%%J"
)
If "%Type%" == "CLASSIC" …
只需根据需要将最后一行中的 …
替换为其余的预期命令。
嗨,谢谢大家的意见,在我阅读这里的回复之前,我实际上想出了一些不同的东西,因为日志文件是用 UCS-2-LE 编码的,所以代码不会读取文件,所以我改变了使用以下命令将其转换为 UTF。
powershell -command "Get-Content C:\Install\Logs\*LOAD-BOOTS_ENVIRONMENT.log" > C:\Install\Logs\Boots_logfile.txt
此外,日志文件以 date/time 开头附加到文件名的开头,例如 20210311215550LOAD-BOOTS_ENVIRONMENT 所以我将其转换为名称为 Boots_logfile.txt.[=15= 的 UTF ]
所以在这个文件中有一行如下所示的文本(行号并不总是相同的)
11-03-2021 21:56:13:58 INFO: [SET_STORE_NUMBER] Name:Manchester
11-03-2021 21:57:02:71 INFO: [SET_APP_VARS] SET Country:UK
11-03-2021 21:57:02:74 INFO: [SET_APP_VARS] SET Language:ENGLISH
11-03-2021 21:57:02:81 INFO: [SET_APP_VARS] Key:STORENUMBER Value:120
11-03-2021 21:57:02:89 INFO: [SET_APP_VARS] Key:IPADDRESS Value:10.10.10.10
所以我写了下面的代码来查找商店名称曼彻斯特
set torun=findstr /c:"[SET_STORE_NUMBER] Name" "C:\Install\Logs\Boots_logfile.txt"
for /f "tokens=1,2,3,4,5,6 delims=:" %%a in ('%torun%') do set boots001="%%f"
并用echo Store Name%TAB%%TAB%%boots001%
显示出来
这看起来是否正确,或者是否有更短的方法,所以如上所述,我将使用 %boots002% %boots003%
等
获取商店名称、电话号码、IP 地址等
我正在尝试查找并使用特定单词作为变量,以使用批处理脚本打印到 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 _
根据我认为您可能正在做的事情,我可能会提供不同的方法:
@Echo Off
SetLocal EnableExtensions
For /F "UseBackQ Tokens=1,* Delims=]" %%G In ("test.txt") Do (
For /F "Tokens=1,* Delims=: " %%I In ("%%H") Do Echo Set "%%I=%%J"
)
If "%Type%" == "CLASSIC" …
只需根据需要将最后一行中的 …
替换为其余的预期命令。
嗨,谢谢大家的意见,在我阅读这里的回复之前,我实际上想出了一些不同的东西,因为日志文件是用 UCS-2-LE 编码的,所以代码不会读取文件,所以我改变了使用以下命令将其转换为 UTF。
powershell -command "Get-Content C:\Install\Logs\*LOAD-BOOTS_ENVIRONMENT.log" > C:\Install\Logs\Boots_logfile.txt
此外,日志文件以 date/time 开头附加到文件名的开头,例如 20210311215550LOAD-BOOTS_ENVIRONMENT 所以我将其转换为名称为 Boots_logfile.txt.[=15= 的 UTF ]
所以在这个文件中有一行如下所示的文本(行号并不总是相同的)
11-03-2021 21:56:13:58 INFO: [SET_STORE_NUMBER] Name:Manchester
11-03-2021 21:57:02:71 INFO: [SET_APP_VARS] SET Country:UK
11-03-2021 21:57:02:74 INFO: [SET_APP_VARS] SET Language:ENGLISH
11-03-2021 21:57:02:81 INFO: [SET_APP_VARS] Key:STORENUMBER Value:120
11-03-2021 21:57:02:89 INFO: [SET_APP_VARS] Key:IPADDRESS Value:10.10.10.10
所以我写了下面的代码来查找商店名称曼彻斯特
set torun=findstr /c:"[SET_STORE_NUMBER] Name" "C:\Install\Logs\Boots_logfile.txt"
for /f "tokens=1,2,3,4,5,6 delims=:" %%a in ('%torun%') do set boots001="%%f"
并用echo Store Name%TAB%%TAB%%boots001%
显示出来
这看起来是否正确,或者是否有更短的方法,所以如上所述,我将使用 %boots002% %boots003%
等