【问题标题】:Rename parts of file names that match a pattern重命名与模式匹配的文件名部分
【发布时间】:2016-08-24 05:36:18
【问题描述】:

假设我有一个这样的文件名列表:

some-file.ts  
my-project-service.ts  
other.ts  
something-my-project.ts 

我需要更改其中包含my-project 的文件名,以便将那部分重命名为$appname$

所以my-project-service.ts 会变成$appname$-service.ts

我需要从根目录递归地执行此操作。

我似乎对 PowerShell 绝望了,所以我想我会在这里问一下是否有人可以帮助我。

【问题讨论】:

  • This 可能会对您有所帮助。
  • @MartinBrandl 感谢您的精彩回答!

标签: powershell powershell-2.0


【解决方案1】:

使用Get-ChildItem cmdlet 和-recurse 开关以递归方式从根目录获取所有项目。使用Where-Object cmdlet 过滤所有包含my-project 的项目,最后使用Rename-Item cmdlet 重命名它们:

Get-ChildItem "D:\tmp" -Recurse | 
    Where {$_.Name -Match 'my-project'} | 
    Rename-Item -NewName {$_.name -replace 'my-project','$appname$' } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多