1

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.

chandu
  • 375
  • 3
  • 16
  • related: http://stackoverflow.com/questions/30195025/increment-version-in-a-text-file – Anthony Stringer Jun 16 '16 at 16:21
  • 1
    Can you show us something you have tried to accomplish this? Once we see what you have tried we can build on this process and show what changes need to be made. Right now this reads like a code writing request which is off topic. We want to help but it is expected that you have tried something first. – Matt Jun 16 '16 at 17:04
  • What format is the file in and is that _exactly_ how the text appears in the file? – Matt Jun 16 '16 at 17:06
  • @Antony, I checked , the text file has only version nothing else. in my case, there is some other text also. – chandu Jun 16 '16 at 19:29
  • @Matt, its "fileName.reg" but we can treat it as normal text file. Inside something like this : Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\application ] [HKEY_LOCAL_MACHINE\SOFTWARE\something] "data"="C:\\ProgramData\\somename" [HKEY_LOCAL_MACHINE\SOFTWARE\someothername] "data"="C:\\ProgramData\\name\\somename " [HKEY_LOCAL_MACHINE\SOFTWARE\some name] "Version"="1.3.0" – chandu Jun 16 '16 at 19:31
  • The linked question tells you how to manipulate the version. That is why I have not closed this as an _exact_ duplicate. The only other concern is that you need to extract that one part of the text. For instance `Get-Content file | Select -Last 1` would get the last line. There is more to it than that but please try something on your own first and report back here. I am sure searching for "edit one line in file" and then "edit version in string" would get you what you need. – Matt Jun 16 '16 at 19:35
  • $Verfile = "C:\Work\fileNmae.reg" $curVer = (Get-Content $Verfile | Select -Last 1) $tokens = $curver.Split(".") $minor = [int]($tokens[1]) $new_minor = $minor + 1 $newVer = $tokens[0] +"."+ $new_minor +"." + $tokens[2] (gc $Verfile) -replace "$curVer", "$newVer" | sc $Verfile – chandu Jun 16 '16 at 20:16
  • managed to get it working, but i am not sure if its best way of doing. But its working :-) Please suggest some efficient way. if possible. Thanks – chandu Jun 16 '16 at 20:20

2 Answers2

1

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"

Community
  • 1
  • 1
TravisEz13
  • 2,263
  • 1
  • 20
  • 28
  • i tried your code. i got Method invocation failed because [System.Version] doesn't contain a method named 'new'. At C:\Work\scripts\versionupdate.ps1:13 char:35 + $newVersionString = [version]::new <<<< ($version.Major,$version.Minor+1,$version.Build).ToString() + CategoryInfo : InvalidOperation: (new:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound – chandu Jun 17 '16 at 07:28
  • I've updated the example to work with Pre-V5 PowerShell. – TravisEz13 Jun 17 '16 at 20:10
1

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
chandu
  • 375
  • 3
  • 16