0

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 

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 

The variable should have the value 138527011 in this case.

3
  • Ok, here we go again. :) to set the variable is not the issue, you wanted to perform other items when variable is found...anyway, you just want the variable? THen if so, I can do that for you. Commented Feb 15, 2019 at 14:35
  • @GerhardBarnard , yes, but after getting the variable i was going to use IF statements to make comparisons. I am felling like i am just wasting your time, but do you have any tips of how i should solve this? Commented Feb 15, 2019 at 14:40
  • For those wishing to see the previously deleted question, (and who can), it is here. Commented Feb 15, 2019 at 14:55

2 Answers 2

2

Here you go:

@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.

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 
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, but for some reason im getting "the system could not find the file specified"
change your path in the for loop to C:\dev\AssayInfo.txt
I updated the answer with your path, copy as is and try.
0

Personally, I would use:

@echo off for /F "tokens=2" %%A IN ('findstr /ibc:"CouId: " C:\dev\AssayInfo.txt') do set "num=%%A" rem Do some code here: pause exit /b 0 

which is much shorter.

Note that direct comparisons can be done directly inside the for loop like:

@echo off setlocal EnableDelayedExpansion for /F "tokens=2" %%A IN ('findstr /ibc:"CouId: " C:\dev\AssayInfo.txt') do ( set "num=%%A" rem Do sum comparisons with '!num!', not '%num%'!: ) pause exit /b %errorlevel% 

2 Comments

Using type here reminds me at the Useless Use of Cat Award ;-) for /F "tokens=2" %%A IN ('findstr /ibc:"CouId: " C:\dev\AssayInfo.txt') do set "num=%%A"
There are situations, where type is neccessary: when processing Unicode files (test with wmic bios get serialnumber >test.txt and both for /f %a in ('findstr "[a-z]" test.txt') do @echo %a and for /f %a in ('type test.txt^|findstr "[a-z]"') do @echo %a)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.