3

I have a text file (vernum.txt) containing one line:

At revision 2.

How do I use dos cmd line to read in the line and save a variable with JUST the number? It will always be "At Revision ####."

2 Answers 2

3
for /f "tokens=3delims=. " %%i in (vernum.txt) do set vernum=%%i

echo version number=%vernum%

You may need to change vernum.txt to the full pathname if the file is not in the current directory. If your filename contains spaces, the name needs to be quoted using "double quotes". If you use quotes, you'd also need to add the key directive usebackq into the quote before the tokens keyword

The space before the closing quote in the qualifier is significant - it means "delimiters are . or space. The tokens=3 means the third token : At revision and 2

See for /? from the prompt for documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, that answer fails since it returns "3." rather than "3"
2

By using a command such as this:

@ECHO off

SET /P MYVAR=<vernum.txt
ECHO MYVAR=%MYVAR%
FOR /f "tokens=3* delims=.\ " %%K IN ( "%MYVAR%" ) DO (
    SET /A RESULT=%%K
)
ECHO The number is: %RESULT%

pause

1 Comment

Hello, Im new to dos cmd line and I need a very similar solution. Im parsing a larger file and trying to pull out a different number. Would you mind quickly explaining your code. Mainly the for loop so I can understand it and edit it for me needs. @djangofan

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.