Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get output of a FOR loop into a variable in a batch file
    primarykey
    data
    text
    <p>I have a file like this, that needs to be parsed and needs to extract a version number.</p> <pre><code>[assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible [assembly: AssemblyFileVersion("0.4.2")] ... ... </code></pre> <p>I need to parse it and extract the <code>0.4.2</code> from it.</p> <p>I have to do this in windows batch file only. I did this the following way, but I know this is <strong>NOT</strong> the best way.</p> <pre><code>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 &gt;&gt; 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" &gt;&gt; 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! </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload