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

Vac*_*ano 5 powershell powershell-2.0

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

some-file.ts  
my-project-service.ts  
other.ts  
something-my-project.ts 
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Mar*_*ndl 6

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

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