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

前端之家收集整理的这篇文章主要介绍了windows-7 – 如何使用PowerShell递归替换文件和文件和文件夹名称中的字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Power Shell(虽然欢迎其他建议),如何递归循环目录/文件夹和

>在所有文件中用文本A替换B,
>重命名所有文件,使A替换为B,最后替换
>还重命名所有文件夹,以便A替换为B?

通过一些需求改进,我最终得到了这个脚本:
  1. $match = "MyAssembly"
  2. $replacement = Read-Host "Please enter a solution name"
  3.  
  4. $files = Get-ChildItem $(get-location) -filter *MyAssembly* -Recurse
  5.  
  6. $files |
  7. Sort-Object -Descending -Property { $_.FullName } |
  8. Rename-Item -newname { $_.name -replace $match,$replacement } -force
  9.  
  10.  
  11.  
  12. $files = Get-ChildItem $(get-location) -include *.cs,*.csproj,*.sln -Recurse
  13.  
  14. foreach($file in $files)
  15. {
  16. ((Get-Content $file.fullname) -creplace $match,$replacement) | set-content $file.fullname
  17. }
  18.  
  19. read-host -prompt "Done! Press any key to close."

猜你在找的Windows相关文章