replacing text in a txt file.

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Jesse Jesse 1 year, 9 months ago.

  • Author
    Posts
  • #22714
    Profile photo of Jesse
    Jesse
    Participant

    So we have recent come across a situation where we need to replace a line of text in a txt file (config file). We have the following PS code, it opens the file, appears to process the command, closes and saves the file (at least the time stamp has updated).

    $old = ' "enabled_labs_experiments": [ ],'
    $new = ' "enabled_labs_experiments": [ "disable-direct-write" ],'
    (Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State – Copy") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State – Copy"

    We do not need the variables but we did it that way instead of inline so that we could more easily see what we are changing.

    Basically nothing gets updated in this file. There are other places in this same file that we can update the code and it works perfectly so I have a feeling that it has to do with the [ ] or the : or some other character that PS uses. Unfortunately we cannot omit the : or the [ ] due to that being exactly what we need to change in the file.

    The following works perfectly
    $old="USA"
    $new="en"
    (Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State"

    Any suggestions are greatly appreciated!!

  • #22715
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Jesse,

    This was an interesting on to work out. I couldn't understand why I was getting an error when the path looked perfect. Found it, though it's barely noticable.

    (Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State [b]–[/b] Copy") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State [b]–[/b] Copy"

    You want to replace the hypthens, they're slightly different characters. Just select them, and type a [b]-[/b].

  • #22723
    Profile photo of Jesse
    Jesse
    Participant

    Tim,

    Thank you for your response, I may have added some complexity to my example that was not needed. The final product will actually not have a – in the file name like this:

    $old = '"enabled_labs_experiments": [ ],'
    $new = '"enabled_labs_experiments": [ "disable-direct-write" ],'
    (Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State"

    Th actual value that we want to replace is the following line:
    "enabled_labs_experiments": [ ],

    and replace it with:

    "enabled_labs_experiments": [ "disable-direct-write" ],

    in the text file (actually a config file) called:
    Local State

    that is located in the directory:
    C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\

  • #22731
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    OK, second time lucky! 🙂

    I think it's because the -replace operator is interpreting the string as containing a special regex character, the opening square bracket.

    When I change it to use the Replace method of a string, it worked OK.

    ForEach-Object {$_.replace($old, $new)}

  • #22733
    Profile photo of Jesse
    Jesse
    Participant

    You got it Tim.

    Thank you so much for your help!

You must be logged in to reply to this topic.