Getting substring from string with powershell

I want to extract verssion number from MFC *.rc file. It looks like:

  VALUE "FileVersion", "1.22.333.4444\0"

actually I need two values - version 1.22.333.4444 and major version 1.22

I wrote the code below and it gives me the version, but it looks ugly

  $version = Get-Content -Path $rcPath | Select-String -Pattern 'FileVersion' -CaseSensitive –SimpleMatch -List | %{$_ -replace '[\\0]', ''} | %{$_ -replace '[^\d.]', ''}

So my questions are:

  • Is there any simple way to obtain the version?
  • How I can get the major version?

Answers


There's a [Version] type you can use for that:

$text = 'VALUE "FileVersion", "1.22.333.4444\0"'
$version = [version]($text -replace '^.+?([0-9.]+)\\.+','$1')
$version


Major  Minor  Build  Revision
-----  -----  -----  --------
1      22     333    4444    

Then:

$version.ToString()

1.22.333.4444

'{0}.{1}' -f $version.major,$version.minor

1.22

Need Your Help

Get highest value from associative array foreach

php arrays foreach max associative-array

I'm implementing the first answer shown here: Finding Highest Value in Associative Array

Error accessing the Cache in the Application_Start method of Global.asax

c# asp.net iis global-asax

I am trying to access the Cache in the Application_Start method of Global.asax. I am trying to accdess Context.Cache. When I run the code from visual studio it works just fine but when I run it on ...