Home > Blockchain >  How to add zero in front of a variable in a batch file?
How to add zero in front of a variable in a batch file?

Time:04-29

I need to add a zero in front of a variable, but I need to compare two variables before making it. After that, another treatment will reduce the number of digits in each variable, but first I need them to be intact. Why? because in my actual Batch File, my latest version won't being installed because it sees the latest version like if it was older than the installed one and it is false.

It happens because the reduction only keep the first 5 digits of each variable. In this problem, the variable of the installed version became greater than the newest version and of course I don't want this, that is why I want to add a zero in front of the installed one but only if the number of digit are not the same like in this case.

In my treatment, I extract the installed version number and place it into a variable and also for the newest version then I remove the dots in both and I reduce the numbers of digit to 5 or else when it comes to compare it won't work if the variable are too long. So I had this idea below to adjust the number of digit in both variable before the reducing but this part doesn't work.

**Original Version before treatment : Installed_Version = 98.0.4758.82 and Version = 100.0.4896.88

set /A Trimmed_Installed_Version = 980475882
set /A Trimmed_Version = 1000489688
set /A Niveau = 1
set Compare = %Trimmed_Version% / %Trimmed_Installed_Version%
if "%Compare%" GTR "%Niveau%"
    (
    do set Trimmed_Installed_Version = 0%Trimmed_Installed_Version%
    )
echo %Trimmed_Installed_Version%

As you can see, the first two variables are what I have at this point in my treatment after an echo, I just reported it to this part's code. Any idea how to make this work or else I'm open to a better idea?

CodePudding user response:

Calculating %Trimmed_Version% / %Trimmed_Installed_Version% is fine in theory. Problem is that batch is integer-only, so the value assigned is int(%Trimmed_Version% / %Trimmed_Installed_Version%) or 1. Which is not greater than 1. Hence the if is not true.

There's no reason that you can't use

if %Trimmed_Version% GTR %Trimmed_Installed_Version% (

since both values are <2**32.

Note that if you prefix the string with 0 as you appear intending on, then the value is no longer a valid batch integer, as a leading 0 means that the number is octal and hence must be all-digits and cannot contain 8 or 9.

But by prefixing with a 0 to make the values the same length (all batch variables are strings and may be used as integers if they are (signed) numeric-only) then they can be compared with if "%one%" op "%theother%" (

Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"

Use set "var1=value" for setting STRING values - this avoids problems caused by trailing spaces. Quotes are not needed for setting arithmetic values (set /a`)

It may be worth noting for your purposes that the third element in eg.,
98.0.4758.82 so far has been ever-increasing in Google versions, so perhaps comparing that alone may suffice. The same may not apply to other vendors.

CodePudding user response:

I've tried this:

set Version=100.0.4896.88
set Installed_Version=98.0.4758.82
set Trimmed_Version=%Version:.=%
set Trimmed_Installed_Version=%Installed_Version:.=%
set Trimmed_Version=%Trimmed_Version:~0,-5%
set Trimmed_Installed_Version=%Trimmed_Installed_Version:~0,-5%
set Balance=1
set Diff_Version=%Trimmed_Version%/%Trimmed_Installed_Version%
if "%Diff_Version%" GTR "           
  • Related