I am trying to write an equivalent of
find -name "*.xml" | xargs grep -l "Search String"
| xargs perl -p -i -e 's/Search String/Replace String/g'
in powershell. This is what I came up with.
Get-ChildItem 'D:\code\cpp\FileHandlingCpp\input - Copy' -Recurse |
Select-String -SimpleMatch $src_str |
foreach{(Get-Content $_.path) | ForEach-Object { $_ -replace $src_str, $target_str }}
I get the error "The process cannot access the file because it is being used by another process”. So I came up with the multiple lines version as shown below. I am able to do in-replace of the strings now except the one in $src_str. What's wrong with $src_str ?
$src_str="<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>"
$target_str=""
echo $src_str
foreach ($var in (Get-ChildItem 'D:\code\cpp\FileHandlingCpp\input - Copy' -Recurse
| Select-String -SimpleMatch $src_str).Path)
{
(Get-Content $var) | ForEach-Object { $_ -replace $src_str, $target_str }
| Set-Content $var
}