jean-raphael tremblay
jean-raphael tremblay

Reputation: 11

Need help for trimming variable

Here's my code :

set Installed_Folder_and_Executable=%Local_EXE:\=\\%
set Installed_Version=0
for /f "usebackq delims=, skip=2 tokens=2" %%A in (`wmic datafile where 'name^="%Installed_Folder_and_Executable%"' get Version /value /format:csv`) do set Installed_Version=%%A
::Removing dot from version number
set Trimmed_Version=%Version:.=%
set Trimmed_Installed_Version=%Installed_Version:.=%
::Reduce version number to 5 digits
set Trimmed_Version=%Trimmed_Version:~0,5%
set Trimmed_Installed_Version=%Trimmed_Installed_Version:~0,5%

The purpose of this is: we deploy software to our computers via .bat file and for the utility we need to verify the installed version versus the one we want to install. Everything was working until last week when Google Chrome release a 100.x.xxxx.xx version. Be cause of the way we treat the version number we get some troubles. the last version was 98.x.xxxx.xx, so during the treatment the two versions become 100xx and 98xxx, so it saw the previous version like if it was a more recent one than the 100's one. I was thinking about counting each digit in both variable and if one of them is shorter than add a zero in front of the shortest one.

Did anybody have an idea how to do that, I've tried many way without any result?

Upvotes: 1

Views: 69

Answers (1)

Magoo
Magoo

Reputation: 80213

Consider the following demo using fictitious version numbers

FOR %%v IN (0.0.0.0 98.3.106.51 100.1.4.31 100.0.0.1 98.4.1.336 99.4.12345.338 987.6.54321.876) DO (
 FOR /f "tokens=1-4delims=." %%g IN ("%%v") DO (
 SET /a pre=1000 * %%g + %%h
 SET /a post=100000 * %%i + %%j
 ECHO %%v
 SET pre
 SET post
 )
)

The object is to take the first element and multiply it by 1000 add the second; repeat with the third and fourth.

The consequence is that pre and post will be set to values that will be numerically correct, so a simple comparison if %pre1% gtr %pre2% ... else if %pre1% equ %pre2% if %post1% gtr %post2% ... can be employed to match the version numbers.

If you want to employ strings instead, then simply add 1000000000 to each of the two values, and if %pre1%%post1% gtr %pre2%%post2% ...

The key is the maximum values for the second/fourth element - shift the first/third sufficiently left to be able to accommodate the other in its lower m digits - remembering that batch is limited to numbers < 2**32 so effectively, 9 digits - 10 if a leading 1 is added

This is intended as a general solution to comparison of w.x.y.z values. With specific reference to Chrome, I'd point out that the third group is currently 4 digits long, and appears to be increased for every release (I'd suggest it's a build number) so comparison of that alone should yield the desired result.

Other manufacturers use other schemes

Upvotes: 1

Related Questions