如何使用PowerShell递归替换文件和文件和文件夹名称中的字符串?

Mar*_*R-L 7 powershell windows-7

使用PowerShell(虽然欢迎其他建议),如何递归循环目录/文件夹和

  1. 在所有文件中用文本A替换B,
  2. 重命名所有文件,以便A替换为B,最后替换
  3. 重命名所有文件夹,以便A替换为B?

Mar*_*R-L 13

通过一些需求改进,我最终得到了这个脚本:

$match = "MyAssembly" 
$replacement = Read-Host "Please enter a solution name"

$files = Get-ChildItem $(get-location) -filter *MyAssembly* -Recurse

$files |
    Sort-Object -Descending -Property { $_.FullName } |
    Rename-Item -newname { $_.name -replace $match, $replacement } -force



$files = Get-ChildItem $(get-location) -include *.cs, *.csproj, *.sln -Recurse 

foreach($file in $files) 
{ 
    ((Get-Content $file.fullname) -creplace $match, $replacement) | set-content $file.fullname 
}

read-host -prompt "Done! Press any key to close."
Run Code Online (Sandbox Code Playgroud)