0

I have a requirement to increment to read and increment the pom.xml version by 1 using powershell script.

I was able to fetch the version value as for example: 1.0.123, but the type given here is string, when I try to convert it into Decimal or Double I am getting below error:

Code:

PS C:\Users\XXXX\Downloads> $finale
1.0.153

PS C:\Users\XXXX\Downloads> $finale.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     String                                   System.Object 

Error:

PS C:\Users\XXXX\Downloads> $finale1 = [Double]::Parse($finale)

Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format." At line:1 char:1 + $finale1 = [Double]::Parse($finale) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException

rpm192
  • 2,630
  • 3
  • 20
  • 38
  • 5
    Why do you want it to convert to a Double? Just parse it to a version: `[Version]::Parse("1.0.123")` – guiwhatsthat Jan 18 '19 at 08:41
  • Possible duplicate of [Increment version in a text file](https://stackoverflow.com/questions/30195025/increment-version-in-a-text-file) –  Jan 18 '19 at 09:31
  • You could split the string in three different integers and increment the one you need before recreating your string. – Akaizoku Jan 18 '19 at 11:33
  • @guiwhatsthat I wanted to increment the number, which is why I was searching for conversion – sameervsarma Jan 24 '19 at 15:32

2 Answers2

2

The reason is 1.0.123 is not math. It is nether an integer, nor a double. It is simply a string that contains numbers and symbols. This is why you are getting the error.

See the following Help files: About_Arithmetic_Operators .NET Math Class

Leon Evans
  • 146
  • 1
  • 6
1

Using a [version] type is nice, but it is immutable. This code splits it into an array, increments the third (Build) number, and produces a string in $newfinale.

Note that this does not check to see if there is a third (Build) value. It will produce an exception if the $finale is '1.2'.

PS C:\> $finale = '2.3.4.5'
PS C:\> $a = $finale.split('.')
PS C:\> $a[2] = [int]$a[2] + 1
PS C:\> $newfinale = $a -join '.'
PS C:\> $newfinale
2.3.5.5
lit
  • 14,456
  • 10
  • 65
  • 119