1

I want to remove the path G:\Data\editor\ from a text file and replace it with ..\ using powershell.

How do I go about doing that?

powershell -Command "(gc sample.txt) -replace 'G:\Data\editor\', '..\' | Out-File -encoding ASCII sample.txt"

enter image description here


I had used the answer found here, but it doesn't seem to be working when looking for a path.

JamesRocky
  • 721
  • 9
  • 23

1 Answers1

2

\ is used as the escape character in regular expressions, so in order to represent the verbatim (literal) \ characters in your path, you must use \\:

powershell -Command "(gc sample.txt) -replace 'G:\\Data\\editor\\', '..\' | Out-File -encoding ASCII sample.txt"
mklement0
  • 382,024
  • 64
  • 607
  • 775