Get-childitem 按顺序重命名不同子文件夹中的文件

Get-childitem 按顺序重命名不同子文件夹中的文件

Windows 系统使用 Powershell。

我的电脑上有 5 个文件夹,每个文件夹包含 2 个子文件夹,分别名为 images 和 json,每个 images 子文件夹包含 3 个 png 文件,编号分别为 1.png、2.png 和 3.png。json 子文件夹具有相同的结构,每个子文件夹包含 3 个文件,分别为 1.json、2.json 和 3.json。

我正在做的是从所有图像子文件夹中获取所有 .png 文件并将它们放在一个序列中,即 1.png、2.png、3.png、4.png、5.png、6.png,...

当我使用 -whatif 选项运行命令执行此操作时,文件按照我想要的正确顺序排列,但是当我运行命令生成文件时,子文件夹 2 中的第一个文件跳过了该序列,如下所示:

使用 -whatif 选项在 Powershell 中执行的命令:

PS C:\WINDOWS\system32> $i=1
PS C:\WINDOWS\system32> get-childitem -Path D:\Files_png_json\ -Recurse  -Filter *.png | foreach { rename-item $_.FullName -NewName ('{0}' -f $i++ +$_.extension ) -whatif}
What if: Performing the operation "Rename File" in destination "Item: D:\Files_png_json\Folder 1\Images\1.png Destination: D:\Files_png_json\Folder 1\Images\1.png".
What if: Performing the operation "Rename File" in destination "Item: D:\Files_png_json\Folder 1\Images\2.png Destination: D:\Files_png_json\Folder 1\Images\2.png".
What if: Performing the operation "Rename File" in destination "Item: D:\Files_png_json\Folder 1\Images\3.png Destination: D:\Files_png_json\Folder 1\Images\3.png".
What if: Performing the operation "Rename File" in destination "Item: D:\Files_png_json\Folder 2\Images\1.png Destination: D:\Files_png_json\Folder 2\Images\4.png".
What if: Performing the operation "Rename File" in destination "Item: D:\Files_png_json\Folder 2\Images\2.png Destination: D:\Files_png_json\Folder 2\Images\5.png".
What if: Performing the operation "Rename File" in destination "Item: D:\Files_png_json\Folder 2\Images\3.png Destination: D:\Files_png_json\Folder 2\Images\6.png".

请注意,下面在子文件夹 2 中生成的 png 图像以 5.png 开头。

不带-whatif的命令后生成的文件的名称:

Folder of D:\Files_png_json\Folder 1\Images\

15/03/2022  11:25    <DIR>          .
15/03/2022  11:25    <DIR>          ..
26/02/2022  18:49           276.772 1.png
26/02/2022  18:49           276.772 2.png
26/02/2022  18:49           276.772 3.png


Folder of D:\Files_png_json\Folder 2\Images\

15/03/2022  11:53    <DIR>          .
15/03/2022  11:53    <DIR>          ..
26/02/2022  18:49           276.772 5.png
26/02/2022  18:49           276.772 6.png
26/02/2022  18:49           276.772 7.png

为什么当我运行带有 -whatif 选项的命令时,它显示在子文件夹 2 中将按正确的顺序生成图像,4、5、6.png,而我运行命令后,图像却从数字 5、6 和 7.png 生成?

为什么他跳过 4.png 序列并从 5.png 开始?

答案1

我对跳过文件编号进行了一些测试,并找到了解决方案。在文件选择中替换 -Filter 命令,现在该命令可以正常工作:

'D:\Files_png_json\' | ForEach-Object{$($_ + "\*.png"), $($_ + "\*.json")} | Get-ChildItem -Recurse | foreach { If ( $_.extension -ne $prevExt ) { $i=1 } Rename-Item $_.FullName -NewName ('{0}' -f $i++ +$_.extension); $prevExt = $_.extension; }

注意:由于我的子文件夹有两种类型的文件,png 和 json,因此添加了“if”命令来控制文件扩展名类型,因此当扩展名更改时文件名编号会从数字 1 重置:

例如 1.png、2.png、3.png...1.json、2.json、3.json...

感谢@Vomit IT 提供有关此细节的信息。

输出改变的命令:

 Folder of D:\Files_png_json\Folder 1\Images

15/03/2022  11:25    <DIR>          .
15/03/2022  11:25    <DIR>          ..
26/02/2022  18:49           276.772 1.png
26/02/2022  18:49           276.772 2.png
26/02/2022  18:49           276.772 3.png
 Folder of D:\Files_png_json\Folder 2\Images

16/03/2022  17:38    <DIR>          .
16/03/2022  17:38    <DIR>          ..
26/02/2022  18:49           276.772 4.png
26/02/2022  18:49           276.772 5.png
26/02/2022  18:49           276.772 6.png
 Folder of D:\Files_png_json\Folder 1\Json

16/03/2022  09:19    <DIR>          .
16/03/2022  09:19    <DIR>          ..
26/02/2022  18:49           276.772 1.json
26/02/2022  18:49           276.772 2.json
26/02/2022  18:49           276.772 3.json
 Folder of D:\Files_png_json\Folder 2\Json

16/03/2022  17:38    <DIR>          .
16/03/2022  17:38    <DIR>          ..
26/02/2022  18:49           276.772 4.json
26/02/2022  18:49           276.772 5.json
26/02/2022  18:49           276.772 6.json

相关内容