I am trying to read some info from a text file by using windows command line, and save it to a variable just like \"set info =1234\"
Below is the content of the txt
The following code snippet shows how to do this:
@echo off
setlocal enableextensions enabledelayedexpansion
set revision=
for /f "delims=" %%a in (input.txt) do (
set line=%%a
if "x!line:~0,10!"=="xRevision: " (
set revision=!line:~10!
)
)
echo !revision!
endlocal
Its output is 1234
as desired.
The setlocal
is what I use in every script to ensure variables are treated in a known way. The for
statement processes each line in the input file (the delims
bit stops the line from being tokenised into separate words).
The !line:~
bits are substrings with !line:~0,10!
being the first ten characters and !line:~10!
being the rest.
So, basically, it checks every line to see if it starts with "Revision: "
and, if so, extracts the rest of the line for later.