将字符串值设置为变量
问题描述:
我需要将变量设置为从find命令获得的子字符串,但它会返回命令
I need to set a variable to a substring I get from a find command, but it is returning the command
findstr /is "CouId:" C:\dev\AssayInfo.txt
set number=findstr /is "CouId:" C:\dev\AssayInfo.txt
echo %number%
pause
这就是我正在使用的findstr文件的样子:
This is what the file that I'm using findstr looks like:
Protocol: NVD_RCP_Fluidic_Accuracy_v0.4
ProtocolVersion: 1
SampleId: FLQC+20191126+00111280+96
InstrumentId: 123456789001
CouId: 138527011
CouSlot: 1
在这种情况下,变量的值应为138527011
.
The variable should have the value 138527011
in this case.
答
在这里:
@echo off
for /f "tokens=1,* delims=: " %%i in ('type "C:\dev\AssayInfo.txt" ^| findstr /i CouID') do set "number=%%j"
echo %number%
:# Here you can add your if statements etc.
pause
请注意,根据这个问题,而不是另一个问题,这正是您所需要的,因此,这就是我现在能给您的全部信息.
Note, that this is exactly what you required, as per this question and not the other question and therefore this is all I can give you now.
但是,甚至不需要这样做,您可以在没有set
变量的情况下执行此操作:
However, this is not even needed, you can do it without the set
variable:
@echo off
for /f "tokens=1,* delims=: " %%i in ('type "C:\dev\AssayInfo.txt" ^| findstr /i CouID') do (
echo %%i
:# Here you can add your if statements etc.
)
pause