How to get next nearest version using batch file | Software versioning

69 views Asked by At

I am looking for a batch file script that can get me previous available version from a list of versions string.

Example: ListOfVersion: "16.5.0.0,16.5.1.0,16.5.2.0,16.6.0.0,17.2.0.0" CurrentVersion : "16.5.1.4"

Above are the available values, now I want a bat file logic to get previous available versions from the list as below

PreviousAvailableVersion : "16.5.1.0"

Thanks for the answers.

More Examples "16.5.0.0,16.5.1.0,16.5.2.0,16.6.0.0,17.2.0.0"

IF "16.5.0.1" is installed version then I am expecting prev version to be 16.5.0.0 IF "16.6.1.1" is installed version then I am expecting prev version to be 16.6.0.0

Here is some idea I thought of.. we can have some logic to loop and decrement current version and compare with that list and matching result should be the answer.

Many thanks

2

There are 2 answers

0
Mofi On

The batch file below produces the expected result under following conditions:

  1. Neither the current version string nor any version string inside the list of versions should contain ever a different character than a dot and digits 0-9.
  2. The list of versions is already in correct order. The result would be wrong on list of versions is something like 16.5.0.0,17.2.0.0,16.5.1.0,16.6.0.0,16.5.2.0.
  3. The versions in the list are separated by a normal space, comma, semicolon, equal sign or OEM encoded no-break space with decimal code point value 255.
  4. No version contains a number with a leading zero like 16.5.020.08. The Windows Command Processor interprets numbers with a leading 0 as octal numbers. 020 is interpreted therefore as decimal number 16 and 08 as decimal number 0 because of the digits 8 and 9 are invalid in an octal number and zero is used in this error case.

The batch file for determining the previous version is:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ListOfVersions=16.5.0.0,16.5.1.0,16.5.2.0,16.6.0.0,17.2.0.0"
set "PrevMajor=0" & set "PrevMinor=0" & set "PrevPatch=0" & set "PrevBuild=0"
if not "%~1" == "" (set "CurrentVersion=%~1") else set "CurrentVersion=16.5.1.4"
for /F "tokens=1-4 delims=." %%G in ("%CurrentVersion%") do set "CurrentMajor=%%G" & set "CurrentMinor=%%H" & set "CurrentPatch=%%I" & set "CurrentBuild=%%J"

for %%# in (%ListofVersions%) do for /F "tokens=1-4 delims=." %%G in ("%%#") do (
    if %%G GTR !CurrentMajor! goto Output
    if %%G LSS !CurrentMajor! (
        set "PrevMajor=%%G" & set "PrevMinor=%%H" & set "PrevPatch=%%I" & set "PrevBuild=%%J"
    ) else (
        if %%H GTR !CurrentMinor! goto Output
        if %%H LSS !CurrentMinor! (
            set "PrevMajor=%%G" & set "PrevMinor=%%H" & set "PrevPatch=%%I" & set "PrevBuild=%%J"
        ) else (
            if %%I GTR !CurrentPatch! goto Output
            if %%I LSS !CurrentPatch! (
                set "PrevMajor=%%G" & set "PrevMinor=%%H" & set "PrevPatch=%%I" & set "PrevBuild=%%J"
            ) else (
                if %%J GTR !CurrentBuild! goto Output
                if %%J EQU !CurrentBuild! goto Output
                set "PrevMajor=%%G" & set "PrevMinor=%%H" & set "PrevPatch=%%I" & set "PrevBuild=%%J"
            )
        )
    )
)

:Output
echo Current version:  %CurrentVersion%
echo Previous version: %PrevMajor%.%PrevMinor%.%PrevPatch%.%PrevBuild%
endlocal

The batch file can be run from within a command prompt window with a version string as first argument for easy testing it without changing any line in the batch file.

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.

  • call /? ... explains %~1
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?

See also:

0
Aacini On

This simple code gets the expected results accordingly to the displayed examples:

@echo off
setlocal EnableDelayedExpansion

if "%1" equ "" echo Give Current Version in parameter & goto :EOF

set "CurrentVersion=%1"
set "ListOfVersion=16.5.0.0,16.5.1.0,16.5.2.0,16.6.0.0,17.2.0.0"

set "ListOfVersion=%ListOfVersion%,"

set "temp=%ListOfVersion:,=" & IF "!temp!" LSS "!CurrentVersion!" set "PreviousAvailableVersion=!temp!" & set "temp=%"

echo Current Version  = %CurrentVersion%
echo Previous Version = %PreviousAvailableVersion%

This code works correctly as long as all the version numbers have the same number of digits in each part, that is, that all numbers be directly comparable as strings.

If this restriction is not true, the code could be modified in order to manage such different-number-of-digits version numbers