I am trying to make further use of a wonderful piece of code I found when I tried to replace text at a specified line.
However trying to get it to read $_.Substring()
and then using $_ -replace
is giving me troubles; although I get no error messages the text does not get replaced.
Here is code that does not work:
$content = Get-Content Z:\project\folder\subfolder\newindex2.html
$content |
ForEach-Object {
if ($_.ReadCount -ge 169 -and $_.ReadCount -le 171) {
$a = $_.Substring(40,57)
$linked = '<a href="http://tempsite.info/folder1/folder2/' + $a + '">' + $a + '</a>'
$_ -replace $a,$linked
} else {
$_
}
} |
Set-Content Z:\project\folder\subfolder\newindex2.html
The whole point is to make the content of a cell in a html table column link to a file on a webserver with the same name as what's in the cell.
I didn't have any luck trying my hand at regex trying to match the filenames, but since I managed to make it so the text that's to be replaced always ends up at the same position, I figured I'd try positional replacement instead. The text that is to be replaced is always 57 characters long and always starts at position 40.
I looked at the variables getting set, and everything gets set correctly, except that
$_ -replace $a,$linked
does not replace anything. Instead, the whole file just gets written anew with nothing changed. Can anyone please point to what I am missing and/or point to how to reach the result more easily? Maybe I'm using Substring wrong and should be using something else?