I have a file like this, that needs to be parsed and needs to extract a version number.
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
[assembly: AssemblyFileVersion("0.4.2")]
...
...
I need to parse it and extract the 0.4.2
from it.
I have to do this in windows batch file only. I did this the following way, but I know this is NOT the best way.
setlocal enabledelayedexpansion
SET VERSIONSTR=
REM This is an ugly way to do this, because due to some reason I cant save output of for loop to a variable
REM I would like this in a variable instead of a temp file.
FOR /f "tokens=2*" %%j in (AssemblyInfo.cs) do @echo %%j >> tempfile.txt
REM The "AssemblyFileVersion" line has the version number.
REM I would like this in a variable instead of a temp file.
FOR /f %%i in (tempfile.txt) do @echo %%i | find "AssemblyFileVersion" >> anothertempfile.txt
REM Now this for loop will set a variable.
FOR /f %%k in (anothertempfile.txt) do set VERSIONSTR=%%k
REM Now the line is `AssemblyFileVersion("0.4.2")]` so, I am getting the substring from 21st character
开发者_如何学运维REM till the end and then removing last 3 characters, getting us the string 0.4.2
SET VERSIONSTR=!VERSIONSTR:~21,-3!
I hate having this ugliness in my code, any ideas on how I can modify this? At the very least I would like to know a way for me to get the output of a for loop in a variable in windows batch file.
for /f "tokens=2 delims=()" %%a in ('findstr AssemblyFileVersion AssemblyInfo.cs') do set Version=%%~a
echo %Version%
It got a little ugly because I cannot (apparently) use quotation marks as delimiters, so I chose the parentheses and removed the quotes with %%~x
. You can avoid all temporary files here, by the way.
The code above just directly grabs the appropriate line (with findstr
) and tokenizes it with for /f
.
精彩评论