Given these powershell functions below. I'm trying to replace the version text with another version that I specify. All my paths are correct, and I output both the current version and new version appropriatlely. When i go to replace them, the file indicates that it has changed, but no text was changed.
Alternately, I've found if i specify the exact current version as a string variable and bypass the Get-VersionXXX calls, it replaces the version without issues. Is there anything obvious I'm doing wrong?
function Get-VersionAndDateFromFile([string] $versionFile, [string] $versionString)
{
if (-Not(Test-Path -path $versionFile))
{
return ""
}
else
{
#foreach($line in [System.IO.File]::ReadLines($versionFile))
(Get-Content -Path $versionFile -Raw) | ForEach-Object {
$line = $_
if ($line.Contains($versionString))
{
if ($line.Contains(""""))
{
$tokens = $line.Split('\"')
return $tokens[1]
}
}
}
}
return ""
}
function Get-VersionOnlyFromFile([string] $versionFile, [string] $versionString)
{
[string] $versionAndDate = Get-VersionAndDateFromFile $versionFile $versionStriong
return VersionFromVersionAndDateString $versionAndDate
}
function Get-VersionFromVersionAndDateString([string] $versionAndDateString)
{
if (!$versionAndDateString)
{
return ""
}
else
{
if ($versionAndDateString.Contains(" "))
{
$newTokens = $versionAndDateString.Trim().Split(" ")
return $newTokens[0]
}
else
{
return $versionAndDateString
}
}
}
function ReplaceTextInFile([string] $fullPath, [string] $oldString, [string] $newString)
{
Write-Host "Old " $oldString
Write-Host "New " $newString
((Get-Content -path $fullPath -Raw) -replace $oldString,$newString) | Set-Content -Path $fullPath
}
Calling code:
[string] $newVersionString = "1.2.3.4.6 09-16-2021"
[string] $currentVersionString = Get-VersionAndDateFromFile $pathToVersionFile "SW_VERSION"
ReplaceTextInFile -fullPath $pathToVersionFile -oldString $currentVersionString -newString $newVersionString
finally: file is a header file called Version.h
#define SW_VERSION "1.2.3.4.5 09-14-2021"