I am writing a batch file to automate a series of tasks. One of these tasks is to update the version of the dlls in my solution, by editing the assemblyinfo.cs file in the vario
A fairly old question with an accepted answer, but I also (due to limitations on the CI build server) wanted to do this via a batch file instead of powershell or MS Build Tasks. Here is what I came up with - assumes the solution directory is the current directory:
SetVersion.bat
@echo off
setlocal enabledelayedexpansion
if [%1] == [] GOTO :USAGE
set version=%1
set fileversion=%1
set informationalversion=%1
if [%2] NEQ [] SET fileversion=%2
if [%3] NEQ [] SET informationalversion=%3
echo Setting assembly version information
for /F "delims=" %%f in ('dir /s /b Assemblyinfo.cs') do (
echo ^> %%f
pushd %%~dpf
for /F "usebackq delims=" %%g in ("AssemblyInfo.cs") do (
set ln=%%g
set skip=0
if "!ln:AssemblyVersion=!" NEQ "!ln!" set skip=1
if "!ln:AssemblyFileVersion=!" NEQ "!ln!" set skip=1
if "!ln:AssemblyInformationalVersion=!" NEQ "!ln!" set skip=1
if !skip!==0 echo !ln! >> AssemblyInfo.cs.versioned
)
echo [assembly: AssemblyVersion^("%version%"^)] >> AssemblyInfo.cs.versioned
echo [assembly: AssemblyFileVersion^("%fileversion%"^)] >> AssemblyInfo.cs.versioned
echo [assembly: AssemblyInformationalVersion^("%informationalversion%"^)] >> AssemblyInfo.cs.versioned
copy /y AssemblyInfo.cs AssemblyInfo.cs.orig
move /y AssemblyInfo.cs.versioned AssemblyInfo.cs
popd
)
echo Done^^!
GOTO:EOF
:USAGE
echo Usage:
echo.
echo SetVersion.bat Version [FileVersion] [InformationalVersion]
echo.
Explanation
Basically this will recurse all the subfolders and identify AssemblyInfo.cs files, and then line-by-line copy the files to a new file. If any of the assembly version entries are found they are omitted. Then, at the end of each file, it appends the supplied version numbers.
For those batch gurus out there, some might ask why I prefer using for /F
with dir /s
instead of for /R
to iterate over the files. Basically, its because I find the recursive for to be a bit clunky since it will execute for all sub-directories (not just matching files) when no wildcards are supplied. Using the dir
command is more consistent and easier to predict.
Note: This batch file will remove all empty lines from the AssemblyInfo.cs files