重命名与模式匹配的文件名部分

Rename parts of file names that match a pattern

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

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

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

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

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

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

使用带有 -recurse 开关的 Get-ChildItem cmdlet 从根目录递归获取所有项目。使用 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$' }