Batch file 对于/F,这是跳过批处理文件中某一行的正确方法吗
我试图找到并使用一个特定的单词作为变量,使用批处理脚本将其打印到txt文件中 这是文本的一个例子Batch file 对于/F,这是跳过批处理文件中某一行的正确方法吗,batch-file,variables,find,Batch File,Variables,Find,我试图找到并使用一个特定的单词作为变量,使用批处理脚本将其打印到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:
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
):
根据我认为您可能正在做的事情,我可能会提供一种不同的方法:
@Echo关闭
setLocalEnableExtensions
对于/F“UseBackQ Tokens=1,*Delims=]”中的%%G(“test.txt”)Do(
对于/F“Tokens=1,*Delims=:”(%%H”)中的%%I执行回显设置“%%I=%%J”
)
如果“%Type%”==“CLASSIC”…
只需根据需要将最后一行中的
…
替换为其他预期命令。您好,感谢各位的输入,在我阅读这里的回复之前,我实际上想出了一些不同的方法,因为日志文件是用UCS-2-LE编码的,所以代码不会读取该文件,所以我使用下面的命令将其更改为UTF
powershell -command "Get-Content C:\Install\Logs\*LOAD-BOOTS_ENVIRONMENT.log" > C:\Install\Logs\Boots_logfile.txt
此外,日志文件以日期/时间开头,附加到文件名的开头,如20210311215550LOAD-BOOTS_ENVIRONMENT,因此我将其转换为名为BOOTS_logfile.txt的UTF
因此,在这个文件中有一行文本,如下所示(行号并不总是相同)
所以我写了下面的代码来查找商店名Manchester
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商店名称%TAB%%TAB%%boots001%%
显示它
这看起来是正确的还是有一个较短的方法,因此如上所述,我将使用
%boots002%%boots003%
etc%SystemRoot%\System32\find.exe“CLASSIC”0NUL&&Set“Var=CLASSIC”
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"