将字符串值设置为变量 [英] Setting a string value to a variable

查看:90
本文介绍了将字符串值设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将变量设置为从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

这篇关于将字符串值设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆