chandu
chandu

Reputation: 385

Powershell script to increment version in a text file

I have a text file that contains some text and end of the file "Version"="1.3.0"

I would like to increment the build number "Version"="1.4.0". in the same file, without affecting the other contents.

help needed in powershell please.

Upvotes: 1

Views: 2010

Answers (2)

chandu
chandu

Reputation: 385

Thanks a lot @Matt && @TravisEz13 for your tips. with your help here is the another working version.

$Verfile = "C:\Work\Example\versionfile.reg"
$fileContents  = (Get-Content $Verfile | Select -Last 1)

$parts=$fileContents.Split('=')
$versionString = $parts[1].Replace('"','')
$version = [Version]$versionString
$newVersionString = (New-Object -TypeName 'System.Version' -ArgumentList @($version.Major, ($version.Minor+1), $version.Build)).ToString()
(gc $Verfile) -replace "$versionString", "$newVersionString" | sc $Verfile

Upvotes: 1

TravisEz13
TravisEz13

Reputation: 2413

I assume you can parse the file to the point you get to similar content you give in your question. Additionally, my example is lacking in error handling.

Example

$fileContents =  '"Version"="1.4.0"'

$parts=$fileContents.Split('=')
$versionString = $parts[1].Replace('"','')
$version = [Version]$versionString
$newVersionString = (New-Object -TypeName 'System.Version' -ArgumentList @($version.Major, ($version.Minor+1), $version.Build)).ToString()
$fileContents.Replace($versionString,$newVersionString)      

Result

"Version"="1.5.0"

Upvotes: 1

Related Questions