So I can change text in a file like this:
(gc file.name) -replace 'Foo', 'Bar' | sc file.name
Now I want to do this for each file in this directory. In DOS I would do:
for %f in (*.*) do (gc %f) -replace 'Foo', 'Bar' | sc %f
I get a helpful message telling me the syntax is wrong, but it doesn't tell me what the right syntax is. I bet someone here can.
CodePudding user response:
You just need Get-ChildItem
to get the files in the directory and a loop of your choice, ForEach-Object
in this case.
Get-ChildItem path\to\targetDirectory -File | ForEach-Object {
(Get-Content -LiteralPath $_.FullName -Raw) -replace 'Foo', 'Bar' |
Set-Content $_.FullName
}
If you want to do this recursively for all files under targetDirectory
you would just include -Recurse
.
CodePudding user response:
I used:
foreach ($F in dir){ (gc $F) -replace 'Foo', 'Bar' | sc $F }