Reputation: 970
I have a textfile with several different rows. I want to get the number of the row which start with CellNumber e.g.
file.txt
Hello there
my name is struct
CellNumber 4.0050
I am from Timbuktu
How can I store the number 4.005 in a variable?
This is my try:
for /F "tokens=*" %%A in (file.txt) do (
echo %%A
IF "%%A:~0,10%"=="CellNumber" (
set var=%var:~-5%
)
)
echo result: %var
Upvotes: 0
Views: 40
Reputation: 56164
way too complicated. Use find
or findstr
to get the desired line and proper tokens and delimiters to parse that line. All you need is:
for /F "tokens=2" %%A in ('type file.txt^|findstr /b "CellNumber"') do set var=%%A
echo result: %var%
read for /?
for tokens and delimiters.
this avoids/corrects the four errors in your code:
- you can't use substrings with for
variables (%%A
)
- you need delayed expansion
- variables are referenced with %var%
, not %var
- in your example, you need six chars, not five
Upvotes: 3