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

前端 未结 1 467
时光取名叫无心
时光取名叫无心 2021-01-26 00:37

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.

<
相关标签:
1条回答
  • 2021-01-26 01:27

    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 } | ...
    
    0 讨论(0)
提交回复
热议问题