0

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
Struct
  • 950
  • 2
  • 14
  • 41
  • 1
    Possible duplicate of [Assigning a value from a text file to a variable](http://stackoverflow.com/questions/18800484/assigning-a-value-from-a-text-file-to-a-variable) – aschipfl Aug 22 '16 at 17:22

1 Answers1

3

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 forvariables (%%A)
- you need delayed expansion
- variables are referenced with %var%, not %var
- in your example, you need six chars, not five

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91