【问题标题】:Rename-item in multiple subfolders重命名多个子文件夹中的项目
【发布时间】:2017-03-17 14:32:30
【问题描述】:

我有一个软件可以查找名为“report.txt”的文件。但是,文本文件并非全部命名为 report.txt,而且我有数百个子文件夹要查看。

场景:

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

我想在J:\logs 中的所有子文件夹中搜索文件*.txt 并将它们重命名为report.txt

我试过了,但它抱怨路径:

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

【问题讨论】:

    标签: powershell rename


    【解决方案1】:

    Get-ChildItem * 将获取您当前的路径,因此让我们使用定义您想要的路径Get-ChildItem -Path "J:\Logs" 并添加recurse,因为我们想要所有子文件夹中的文件。

    然后让我们添加使用Get-ChildItemincludefile 参数而不是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"}
    

    【讨论】:

    • 谢谢,这正是我想要的。 :)
    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 2018-08-29
    • 2015-10-26
    • 2019-07-23
    • 2016-05-06
    • 2014-04-14
    • 2016-08-03
    • 1970-01-01
    相关资源
    最近更新 更多