重命名多个子文件夹中的项目 [英] Rename-item in multiple subfolders

查看:36
本文介绍了重命名多个子文件夹中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个软件可以查找名为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-ChildItemincludefile 参数而不是 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"}

这篇关于重命名多个子文件夹中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆