Powershell to replace text in multiple files stored in many folders

directory, file, powershell, replace

Solution

$filename is a collection of `System.IO.FileInfo objects`. You have to loop to get the content for each file : this should do what you want :

$filename | %{
    (gc $_) -replace "THIS","THAT" |Set-Content $_.fullname
}

Problem

I want to replace a text in multiple files and folders. The folder name changes, but the filename is always config.xml. ``` $fileName = Get-ChildItem "C:\config\app*\config.xml" -Recurse (Get-Content $fileName) -replace 'this', 'that' | Set-Content $fileName ``` When I run the above script it works, but it writes the whole text in config.xml about 20 times. What's wrong?

Original source

Related problems