0

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?

2
  • What does $a look like? Does it have special regex charaters in the string. That would prevent the match you are looking for. I would like to see what those 3 lines look like from a sample file first.
    – Matt
    Sep 12, 2015 at 14:56
  • Also, If you already know what you are replacing you dont even need regex at all. I would need to see $a I think before I could answer
    – Matt
    Sep 12, 2015 at 15:01

1 Answer 1

1

The first item in the right-hand argument of -replace is a regex pattern, so depending on what the substring contains, some of the characters might be regex control characters.

You can either escape it:

$_ -replace $([regex]::Escape($a)),$linked

Or use the String.Replace() method, which does not use regex:

$_.Replace($a,$linked)

Finally, as @Matt points out, you might want to avoid the find-and-replace approach altogether, since you already know at which character indices you need to insert your new value:

$_.Remove(40,57).Insert(40,$linked)
2
  • This is not right. There is a readcount property. Use GM and you will see. After reading it seems clear what he is doing. His replace is what is question
    – Matt
    Sep 12, 2015 at 14:54
  • This last edit after @Matt made their point resolved the issue. Many thanks! :)
    – Chokehold
    Sep 12, 2015 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.