From this posting, I managed to replace a string in a directory’s subfiles with Powershell on Windows XP.
foreach ($f in gci -r -include "*.bat")
{ (gc $f.fullname) |
foreach { $_ -replace "D:","C:\path" } |
sc $f.fullname
}
Unfortunately, a few exceptional cases required a “C:\\” rather than a “C:\”.
I attempted to use the following command to fix this problem (replacing “C:\path\\” with “C:\\path\\”), but it fails:
foreach ($f in gci -r -include "*.bat")
{ (gc $f.fullname) |
foreach { $_ -replace "C\path\\","C:\\path\\" } |
sc $f.fullname
}
The error is:
Invalid regular expression pattern: C:\path\\
I attempted several variants in an effort to circumvent the problem, but all produce the same error (replacing line #3 above):
foreach { $_ -replace "C:\path\\\\","C:\\path\\" } |
foreach { $_ -replace 'C:\path\\',"C:\\path\\" } |
foreach { $_ -replace "`C:\path\\","C:\\path\\" } |
foreach { $_ -replace 'C:\path\\\\',"C:\\path\\" } |
Any thoughts?
For repalcing replacing “C:\path\” with “C:\\path\\”
use
The Match pattern is regex and ‘\’ needs to be escaped with an ‘\’.
The replace string is just a string, no needs escape characters