2016-11-03 43 views
0

我有一塊軟件,它尋找名爲「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' } 

回答

1

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