user5465830
user5465830

Reputation:

How to replace the content of every file in the directory?

There is a function (Function($_)) that replace all "1" with "2" for each file in the directory. New content is written to the file out.txt.

input: in.txt → 111

output: in.txt → 222 → out.txt

Tell me, please, how to make the replacement take place inside of every file?

Get-Content "C:\Dir\*" | ForEach-Object {Function($_)} > C:\Dir\out.txt

Upvotes: 0

Views: 282

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Get-Content "C:\Dir\*" will give you the content of everything in C:\Dir in one go, so you won't be able to modify each file individually. You'll also get errors for any directory in C:\Dir.

You need to iterate over each file in the directory and process them individually:

Get-ChildItem 'C:\Dir' -File | ForEach-Object {
  $file = $_.FullName
  (Get-Content $file) -replace '1','2' | Set-Content $file
}

The parentheses around Get-Content ensure that the file is read and closed again before further processing, otherwise writing to the (still open) file would fail.

Note that the parameter -File is only supported in PowerShell v3 or newer. On older versions you need to replace Get-ChildItem -File with something like this:

Get-ChildItem 'C:\Dir' | Where-Object { -not $_.PSIsContainer } | ...

Upvotes: 1

Related Questions