1

I have an assembly version in my file AssemblyInfo.cs updated manually by me.
I try to do it automatically. I have set a variable to the current version: 1.0.0.512
This is a string.

Is there a way to increment the version number by 1?

I can successfully update the file automatically, but need to increment the version number.

I tried the following:

@set a="1.0.0.512"
@set /a "c=%a%+1"
echo %c%

But it results in the following error on command line @set /a "c=%a%+1":

Missing operator.

I expect that just the last number is incremented producing for the example above the version string 1.0.0.513.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Debbie.S
  • 167
  • 11
  • Well, `1.0.0.512` is not a signed 32-it integer, which is expected by [`set /A`](https://ss64.com/nt/set.html). *N. B.:* you do not need `%`-signs in a `set /A` expression, `set /A c=a+1` would be enough… – aschipfl Jul 15 '20 at 08:00

2 Answers2

2

The first mistake is on the first command line @set a="1.0.0.512" which defines the environment variable a (meaningless variable name) with the string "1.0.0.512". Yes, the double quotes are also assigned to the environment variable. The correct respectively better syntax would be @set "a=1.0.0.512". See my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explaining the big difference regarding to where first " is placed on a command line with command SET.

The second mistake is not splitting the version number up into four 32-bit signed integer values to increment just the last integer value and than concatenate the four integers again to a version string. 1.0.0.512 is neither a valid integer nor a valid floating point number. So no programming or scripting language supports an increment of last number of this version string with just one command.

The batch code below can be used for version strings in one of the following four formats:

  1. Version like just 1, 2, ...
  2. Major.minor version like 1.2, 2.7, ...
  3. Major.minor.build version like 3.1.47, 8.2.3648, ...
  4. Major.minor.maintenance.build version like 1.3.0.39, 2.7.1.39024, ...

Incremented is the last number of the version string.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FullVersion=%~1"
if not defined FullVersion set "FullVersion=1.0.0.512"
echo Version to increment is: %FullVersion%

rem Make sure that the environment variables used below are
rem not defined already by chance outside the batch file.
set "MajorVersion="
set "MinorVersion="
set "Maintenance="
set "BuildNumber="

rem Split up the version string into four integer values.
for /F "tokens=1-4 delims=." %%I in ("%FullVersion%") do (
    if not "%%L" == "" (
        set "MajorVersion=%%I."
        set "MinorVersion=%%J."
        set "Maintenance=%%K."
        set "BuildNumber=%%L"
    ) else if not "%%K" == "" (
        set "MajorVersion=%%I."
        set "MinorVersion=%%J."
        set "BuildNumber=%%K"
    ) else if not "%%J" == "" (
        set "MajorVersion=%%I."
        set "BuildNumber=%%J"
    ) else set "BuildNumber=%%I"
)

rem Remove leading zeros from build number to get the build number always
rem interpreted as decimal number and never as valid or invalid octal number.
if defined BuildNumber for /F "tokens=* delims=0" %%I in ("%BuildNumber%") do set "BuildNumber=%%I"

rem It is possible that the build number is not defined anymore because of
rem having value 0. But that is no problem on using the arithmetic expression
rem below which uses value 0 for not defined environment variable BuildNumber
rem and so BuildNumber has the correct value 1 after the next command line.
set /A BuildNumber+=1

rem The command lines below can be enabled by removing REM to define
rem the build number with a fixed number of digits like four digits.
REM set "BuildNumber=000%BuildNumber%"
REM set "BuildNumber=%BuildNumber:~-4%"

rem Concatenate the version string together with incremented build number.
set "FullVersion=%MajorVersion%%MinorVersion%%Maintenance%%BuildNumber%"

echo Incremented version is:  %FullVersion%
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

I wrote this simpler solution just for fun...

@echo off
setlocal EnableDelayedExpansion

rem Read the assembly version
set /P "version=" < AssemblyInfo.cs

rem Increment the last number
set "i=1" & set "v1=%version:.=" & set /A i+=1 & set /A "v!i!=%+1"

rem Update the file
> AssemblyInfo.cs echo %v1%.%v2%.%v3%.%v4%

If you want to know how this method works, remove the @echo off command and review the executed code... ;)

Aacini
  • 65,180
  • 12
  • 72
  • 108