重命名项目在多个子文件夹

问题描述:

我有一块软件,它寻找名为“report.txt”的文件。但是,这些文本文件并不都是名为report.txt,并且我有数百个子文件夹可以通过。重命名项目在多个子文件夹

场景:

J:\Logs 
26-09-16\log.txt 
27-09-16\report270916.txt 
28-09-16\report902916.txt 

我想通过所有的子文件夹搜索文件*.txtJ:\logs,并将其重命名为report.txt

我试过,但它抱怨道:

Get-ChildItem * | 
Where-Object { !$_.PSIsContainer } | 
Rename-Item -NewName { $_.name -replace '_$.txt ','report.txt' } 

Get-ChildItem *会得到你的当前路径,所以在这里我们将使用定义要Get-ChildItem -Path "J:\Logs"的路径,并添加recurse,因为我们要在所有文件子文件夹。

然后让我们添加使用Get-ChildItemincludefile参数,而不是Where-Object

那么,如果我们管,为ForEach,我们可以使用重命名,项目的每个对象,该对象重命名将是$_NewName将是report.txt

Get-ChildItem -Path "J:\Logs" -include "*.txt" -file -recurse | ForEach {Rename-Item -Path $_ -NewName "report.txt"} 

我们可以用几个别名,在一个班轮时尚有点修剪下来,靠的位置,而不是列出每个参数

gci "J:\Logs" -include "*.txt" -file -recurse | % {ren $_ "report.txt"} 
+0

感谢,这正是我想要的。 :) – Andy