0

I want to update below property file contents using powershell and replace ${BuildPath} with location = D:\Data\Sample\lib

tibco.alias.junit.jar=${BuildPath}\\junit.jar
tibco.alias.TextDiff.jar=${BuildPath}\\TextDiff.jar
tibco.alias.XMLDiff.jar=${BuildPath}\\XMLDiff.jar

Could some one please help me logic on powershell to update this property file.

Thanks

Praveen
  • 23
  • 1
  • 2

2 Answers2

5

So you have a file with those properties in it yes? Who's to say how it was created or if that is the only contents? Lets just assume you have a file with lots of properties other than that just in case. You want to navigate the file replacing all occurrences* of ${BuildPath} with D:\Data\Sample\lib

$replacePath = "D:\Data\Sample\lib"
$newFile = Get-Content c:\temp\file.ini | ForEach-Object{
    $_ -replace '\${BuildPath}',($replacePath -replace '\\',"\\")
} 
$newFile | Set-Content c:\temp\file.ini

This would give the following output in c:\temp\file.ini

tibco.alias.junit.jar=D:\\Data\\Sample\\lib\\junit.jar
tibco.alias.TextDiff.jar=D:\\Data\\Sample\\lib\\TextDiff.jar
tibco.alias.XMLDiff.jar=D:\\Data\\Sample\\lib\\XMLDiff.jar

* regex will only replace the first occurrence on each line. Doubt there would be more than one anyway.

Matt
  • 45,022
  • 8
  • 78
  • 119
2

If you are rewriting the entire file and you don't need to keep the comments you can use the load syntax here, https://stackoverflow.com/a/20276315/3794873 and then the write syntax here, https://stackoverflow.com/a/35210799/3794873 but mind the notes about losing ordering.

If you need to keep the original order, see the other answer using -replace, or create an OrderedDict in PowerShell and populate it via a loop over the lines in the file (see example in code below).

I've condensed the linked question and answer above in the example below.

$filename = 'myfile.properties'
$filedata = @'
app.name=Test App
app.version=1.2
app.data=Some words
'@

$filedata | set-content $filename


# This method doesn't maintain ordering
$fileProps = convertfrom-stringdata (Get-Content $filename | Out-String) 
#could use also use -raw in PS 3 or higher instead of | Out-String
Write-Output "Initial data"
$fileProps.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Write-Output
$fileProps['app.name'] = 'StringData App'
Write-Output "Updated data"
$fileProps.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Write-Output
$fileProps.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Out-File .\myfile.stringdata.properties -Encoding "ASCII"

# This method uses an ordered dict to maintain... order    
$dict = [ordered]@{}
Get-Content $filename | foreach-object {$dict.add($_.split('=',2)[0],$_.split('=',2)[1])}
Write-Output "Initial data"
$dict.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Write-Output
$dict['app.name'] = 'Ordered Dict App'
Write-Output "Updated data"
$dict.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Write-Output
$dict.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Out-File .\myfile.ordered.properties -Encoding "ASCII"
Community
  • 1
  • 1
dragon788
  • 3,583
  • 1
  • 40
  • 49
  • what if the file contains multiple comments which are equal? It complains that it can't add the key again... – xeruf Dec 23 '17 at 11:02
  • I'm afraid duplicate comments would be tricky and would probably involve needing to track how many times the same comment was "seen" by reading the file line by line in a slightly different manner and appending a number to every instance after the first one before converting to a hash table. – dragon788 Feb 01 '18 at 19:05