[英]Rename-item in multiple subfolders
I have a piece of software which looks for a files named "report.txt". 我有一个软件可以查找名为“ report.txt”的文件。 However, the text files aren't all named report.txt and I have hundreds of sub folders to go through. 但是,这些文本文件并没有全部命名为report.txt,并且我有数百个子文件夹要经过。
Scenario: 场景:
J:\Logs
26-09-16\log.txt
27-09-16\report270916.txt
28-09-16\report902916.txt
I want to search through all the sub folders for the files *.txt
in J:\\logs
and rename them to report.txt
. 我想在所有子文件夹中搜索J:\\logs
*.txt
文件,并将其重命名为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 *
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 *
将获取您的当前路径,因此,让我们使用定义您想要的路径Get-ChildItem -Path "J:\\Logs"
并添加recurse
因为我们希望所有子文件夹中的文件。
Then let's add use the include
and file
parameter of Get-ChildItem
rather than Where-Object
然后让我们添加使用Get-ChildItem
的include
和file
参数,而不是Where-Object
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
. 然后,如果将其通过管道传递给ForEach
,则可以在每个对象上使用Rename-Item,其中要重命名的对象将是$_
,而NewName
将是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"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.