dewalla
dewalla

Reputation: 1327

Parse a simple text file with dos cmd line

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 ####."

Upvotes: 3

Views: 20027

Answers (2)

Magoo
Magoo

Reputation: 79983

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.

Upvotes: 3

djangofan
djangofan

Reputation: 29669

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

Upvotes: 2

Related Questions