重命名多个子文件夹中的项目
我有一个软件可以查找名为report.txt"的文件.但是,文本文件并非都命名为 report.txt,而且我有数百个子文件夹要浏览.
I have a piece of software which looks for a files named "report.txt". However, the text files aren't all named report.txt and I have hundreds of sub folders to go through.
场景:
J:\Logs
26-09-16\log.txt
27-09-16\report270916.txt
28-09-16\report902916.txt
我想在 J:\logs
中搜索文件 *.txt
的所有子文件夹并将它们重命名为 report.txt
.
I want to search through all the sub folders for the files *.txt
in J:\logs
and rename them to report.txt
.
我试过了,但它抱怨路径:
I tried this but it complained about the path:
Get-ChildItem * |
Where-Object { !$_.PSIsContainer } |
Rename-Item -NewName { $_.name -replace '_$.txt ','report.txt' }
Get-ChildItem *
将获取您当前的路径,因此我们使用定义您想要的路径 Get-ChildItem -Path "J:\Logs"
并添加 recurse
因为我们想要所有子文件夹中的文件.
Get-ChildItem *
will get your current path, so in instead let's use the define the path you want Get-ChildItem -Path "J:\Logs"
and add recurse
because we want the files in all the subfolders.
然后让我们添加使用 Get-ChildItem
的 include
和 file
参数而不是 Where-Object
Then let's add use the include
and file
parameter of Get-ChildItem
rather than Where-Object
然后,如果我们将其通过管道传输到 ForEach
,我们可以在每个对象上使用 Rename-Item,其中要重命名的对象将是 $_
和 NewName
将是 report.txt
.
Then if we pipe that to ForEach
, we can use the Rename-Item on each object, where the object to rename will be $_
and the NewName
will be report.txt
.
Get-ChildItem -Path "J:\Logs" -include "*.txt" -file -recurse | ForEach {Rename-Item -Path $_ -NewName "report.txt"}
我们可以使用几个别名以单行方式将其缩减一点,并依赖于位置而不是列出每个参数
We can trim this down a bit in one-liner fashion with a couple aliases and rely on position rather than listing each parameter
gci "J:\Logs" -include "*.txt" -file -recurse | % {ren $_ "report.txt"}