wince
wince

Reputation: 531

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:

Upvotes: 1

Views: 534

Answers (1)

mjolinor
mjolinor

Reputation: 68243

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

Upvotes: 7

Related Questions