我有一个软件可以查找名为“report.txt”的文件。但是,文本文件并非全部命名为 report.txt,而且我有数百个子文件夹要查看。
设想:
J:\Logs
26-09-16\log.txt
27-09-16\report270916.txt
28-09-16\report902916.txt
我想在所有子文件夹中搜索文件
*.txt
在 J:\logs
并将它们重命名为 report.txt
.我试过这个,但它提示路径:
Get-ChildItem * |
Where-Object { !$_.PSIsContainer } |
Rename-Item -NewName { $_.name -replace '_$.txt ','report.txt' }
最佳答案
Get-ChildItem *
将获得您当前的路径,因此让我们使用定义您想要的路径Get-ChildItem -Path "J:\Logs"
并添加 recurse
因为我们想要所有子文件夹中的文件。
然后让我们添加使用 include
和 file
Get-ChildItem
的参数而不是 Where-Object
然后,如果我们通过管道将其发送到 ForEach
,我们可以在每个对象上使用 Rename-Item,其中要重命名的对象将是 $_
和 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"}
关于powershell - 重命名多个子文件夹中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40411126/