Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell:如何按时间顺序和日期对文件进行批量重命名_Powershell - Fatal编程技术网

Powershell:如何按时间顺序和日期对文件进行批量重命名

Powershell:如何按时间顺序和日期对文件进行批量重命名,powershell,Powershell,我有1000多张照片,我想重新命名为“ngimg-”因此应该会产生“ngimg-1,ngimg-2,ngimg-3…”,一直到最后一张照片 我想将PowerShell用于此任务,而不是安装第三方应用程序 我在这里尝试过这段代码,但它不适合此任务 Dir *.jpg | sort Date | ForEach-Object -begin { $count=1 } -process { rename-item $_ -NewName "image$count.jpg"; $co

我有1000多张照片,我想重新命名为“ngimg-”因此应该会产生“ngimg-1,ngimg-2,ngimg-3…”,一直到最后一张照片

我想将PowerShell用于此任务,而不是安装第三方应用程序

我在这里尝试过这段代码,但它不适合此任务

Dir *.jpg | sort Date | ForEach-Object -begin { $count=1 }  -process { rename-item $_ -NewName "image$count.jpg"; $count++ }
我不确定上面的代码是否正确。我只是根据这个编出来的:

Dir *.jpg | ForEach-Object  -begin { $count=1 }  -process { rename-item $_ -NewName "image$count.jpg"; $count++ }
我将感谢任何帮助。多谢各位

更新:为了补充我的问题,我想先将文件按日期排序,然后重命名,因此,应该是这样的:

01/01/2020 = ngimg-1
01/02/2020 = ngimg-2
01/03/2020 = ngimg-3
...and so on
$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

我将用4位数字格式化计数数字,因为您有(超过)1000个图像,所以重命名后它们将正确排序

大概是这样的:

01/01/2020 = ngimg-1
01/02/2020 = ngimg-2
01/03/2020 = ngimg-3
...and so on
$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
如果您看到该代码,请取下
-WhatIf
安全开关,该代码将按照您的意愿重命名所有文件。打开tha开关后,实际上不会重命名任何文件

此处需要
$script:count++
,因为否则,
重命名项
的scriptblock不知道
$count
变量,因此每个文件上的索引号不会增加

当然,将路径
'D:\Test'
更改为图像文件所在的文件夹路径


在这里演示两个屏幕截图

之前:

正如您在第二列(日期)中看到的,文件现在按名称排序,因此文件日期不按时间顺序排列

之后:


重命名后,日期是按时间顺序排列的,但我们仍然会显示按名称排序的列表。

这或多或少应该是可行的。我会在第一部分的周围加上括号,这样它会首先完成(排序可能也会做同样的事情)。我假设您希望按lastwritetime排序,因为您没有在问题中声明它

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".


正如@js2010所建议的,我认为您需要确保
排序操作在管道化到重命名之前完成。下面的括号很容易做到这一点

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

有这么多图片,它可能正在尝试批量执行。括号确保它在开始重命名之前完成排序。

Hi@Theo非常感谢您抽出时间来帮助我。您共享的代码是完美的!然而,我确实忘了在我的问题中提到,对我来说真正重要的是,文件在按创建时间排序后被重命名。例如,ngimg-1=01/01/2020 ngimg-2=01/02/2020 ngimg-3=01/03/2020如果您有更多的空闲时间,如果您能更新上面共享的代码,我将不胜感激:)@TheAuthor在这种情况下,请使用
CreationTime
而不是
LastWriteTime
我复制了上面的代码并用CreationTime更新了它,但它仍然随机重命名文件(不是按升序,日期为mm/dd/yyyy)。:(我真的希望按日期升序重命名这些文件,这样最旧的文件将是#1,最新的文件将是最后一个,以及介于两者之间的所有其他文件。你能告诉我怎么做吗?@TheAuthor好吧,我的代码按文件的
LastWriteTime
属性对文件进行排序,以便最旧的文件位于顶部。如果你想这样做的话t反转,只需在此处添加
-Descending
开关。LastWriteTime属性是您在资源管理器中看到的修改日期。您提到的
MM/dd/yyyyy
日期格式不是可排序的格式。为此,您需要
yyyy/MM/dd