简体   繁体   English

Powershell:如何按时间顺序批量重命名文件并按日期排序

[英]Powershell: How to batch rename files in chronological order and sorted by Date

I have more than 1000 photos that I want to rename into "ngimg-" therefore should yield "ngimg-1, ngimg-2, ngimg-3..." and on and on until the last photo.我有 1000 多张照片要重命名为“ngimg-”,因此应该产生“ngimg-1、ngimg-2、ngimg-3...”,直到最后一张照片。

I would like to use PowerShell for this task instead of installing third party applications.我想使用 PowerShell 来完成此任务,而不是安装第三方应用程序。

I've tried this code here but it's not the proper one for this task我在这里尝试过这段代码,但它不适合这项任务

Dir *.jpg | sort Date | ForEach-Object -begin { $count=1 }  -process { rename-item $_ -NewName "image$count.jpg"; $count++ }

I'm not sure if that code above is right.我不确定上面的代码是否正确。 I just made it up from this:我只是从这个弥补的:

Dir *.jpg | ForEach-Object  -begin { $count=1 }  -process { rename-item $_ -NewName "image$count.jpg"; $count++ }

I would appreciate any help.我将不胜感激任何帮助。 Thank you谢谢

Update: To add to my question, I want to have the files sorted by date first and then be renamed, therefore, it should be like this:更新:要添加到我的问题,我想先按日期排序文件,然后重命名,因此,它应该是这样的:

01/01/2020 = ngimg-1
01/02/2020 = ngimg-2
01/03/2020 = ngimg-3
...and so on

I would format the count number with 4 digits, since you have (more than) 1000 images, so they will sort correctly after renaming them.我会用 4 位数字格式化计数,因为你有(超过)1000 张图像,所以在重命名它们后它们会正确排序。

Something like this:像这样的东西:

$count = 1
Get-ChildItem -Path 'D:\Test' -Filter '*.jpg' -File |
Sort-Object LastWriteTime |
Rename-Item -NewName { 'ngimg-{0:D4}{1}' -f $script:count++, $_.Extension } -WhatIf

Take off the -WhatIf safety switch if you have seen the code would have renamed all files as you like.如果您已经看到代码会按照您的喜好重命名所有文件,请取下-WhatIf安全开关。 With tha switch on, no file is actually renamed.打开 tha 开关后,实际上没有文件被重命名。

The $script:count++ is needed here because otherwise, the scriptblock for Rename-Item does not know the $count variable and then the index number will not be incremented on each file.此处需要$script:count++ ,否则, Rename-Item的脚本块不知道$count变量,因此索引号不会在每个文件上递增。

Of course, change the path 'D:\Test' into the folder path where your image files are..当然,将路径'D:\Test'更改为图像文件所在的文件夹路径..


To demonstrate here two screenshots.在这里演示两个屏幕截图。

Before:前:

在此处输入图像描述

As you can see in the second column (Date) the files are now shown sorted by Name, so the file dates are not in chronoligical order.正如您在第二列(日期)中看到的,文件现在显示为按名称排序,因此文件日期不是按时间顺序排列的。

After:后:

在此处输入图像描述

After renaming, the dates ARE in chronological order while still we show the list sorted by Name.重命名后,日期按时间顺序排列,而我们仍然显示按名称排序的列表。

As suggested by @js2010 I think you need to make sure your Sort operation finishes before pipelining it to the rename.正如@js2010 所建议的那样,我认为您需要确保您的Sort操作完成,然后再将其流水线化以进行重命名。 This is easily done with parenthesis like below.这很容易用下面的括号完成。

$count = 1
(Get-ChildItem -Path 'D:\Test' -Filter '*.jpg' -File | Sort CreationTime) | Rename-Item -NewName { 'ngimg-{0:D4}{1}' -f $script:count++, $_.Extension } -WhatIf

With so many pictures it might be trying to batch the execution.有这么多图片,它可能正在尝试批量执行。 The parenthesis makes sure it finishes sorting before it starts renaming.括号确保它在开始重命名之前完成排序。

That should work, more or less.这应该工作,或多或少。 I would put parentheses around the first part so it finishes first (the sorting probably does the same thing).我会在第一部分周围加上括号,以便它首先完成(排序可能做同样的事情)。 I assume you want to sort by lastwritetime, since you don't state it in the question.我假设您想按 lastwritetime 排序,因为您在问题中没有 state 它。

dir | select name,LastWriteTime

Name      LastWriteTime
----      -------------
file1.jpg 7/1/2020 10:19:26 AM
file2.jpg 7/1/2020 10:19:30 AM
file3.jpg 7/1/2020 10:19:33 AM


(Dir *.jpg) | sort LastWriteTime | ForEach { $count=1 } { rename-item $_ -NewName ngimg-$count.jpg -whatif; $count++ }

What if: Performing the operation "Rename File" on target "Item: C:\Users\js\foo\file1.jpg Destination: C:\Users\js\foo\ngimg-1.jpg".
What if: Performing the operation "Rename File" on target "Item: C:\Users\js\foo\file2.jpg Destination: C:\Users\js\foo\ngimg-2.jpg".
What if: Performing the operation "Rename File" on target "Item: C:\Users\js\foo\file3.jpg Destination: C:\Users\js\foo\ngimg-3.jpg".

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM