powershell - 重命名多个子文件夹中的项目

标签 powershell rename

我有一个软件可以查找名为“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' }

最佳答案

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

然后让我们添加使用 includefile 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/

相关文章:

.net - 在.Net中,有没有办法让 [System.IO.File]::ReadAllLines($file) 读取部分锁定的文件?

powershell - 如何使用 Powershell 中的 MSDeploy 将 Web 应用程序 zip 包部署到 Azure?

xslt - 使用 xslt 重命名节点和值

powershell - Powershell中的模拟-访问远程文件

Powershell Write-Zip 部分文件夹树

powershell - 更改 Powershell session 的当前文化,特定于 v3.0+

Bash 脚本/命令从文件名中批量删除 "@2x"(视网膜图像 -> 正常)

r - 根据 dplyr 与另一个数据帧的匹配更改数据帧中的列名

php - 哪个 IDE 可以正确重构(重命名)Zend Framework 2 类?

linux - 在列表上使用 mv 命令并重命名每个文件