Windows 如何批量重命名文件,包括编号和修改日期?
编辑:我应该选择拍摄日期,因为对于我们正在处理的照片,修改日期有时会缩短一个小时 我正在尝试编写将文件重命名为以下格式的内容:Windows 如何批量重命名文件,包括编号和修改日期?,windows,powershell,batch-rename,Windows,Powershell,Batch Rename,编辑:我应该选择拍摄日期,因为对于我们正在处理的照片,修改日期有时会缩短一个小时 我正在尝试编写将文件重命名为以下格式的内容: 24024 25-12-2014 20.18.JPG 24025 26-12-2014 18.01.JPG 24026 26-12-2014 18.01.JPG 24027 30-12-2014 17.05.JPG 24028 31-12-2014 15.09.JPG 24029 31-12-2014 15.19.JPG 我需要这个来按照我父亲设计的方式组织我母亲的照
24024 25-12-2014 20.18.JPG
24025 26-12-2014 18.01.JPG
24026 26-12-2014 18.01.JPG
24027 30-12-2014 17.05.JPG
24028 31-12-2014 15.09.JPG
24029 31-12-2014 15.19.JPG
我需要这个来按照我父亲设计的方式组织我母亲的照片。我首先专门寻找使用cmd批处理文件的方法,但它似乎太复杂了。我现在正在尝试使用PowerShell
我已经尝试过这个方法,效果很好:
Get ChildItem*.JPG | Rename Item-newname{$\ LastWriteTime.toString(“dd-MM-yyy-HH.MM”)+“.JPG”}
但我还没有成功地将变量计数包括在内。这不会编译:
$a=10;获取ChildItem*.JPG |{Rename Item-newname{$\ LastWriteTime.toString(“dd-MM-yyy-HH.MM”)+“.JPG”};$a++}
我在另一个问题中发现了这一点
Foreach($getchilditem*.JPG中的Item){Rename Item-newname{$$\LastWriteTime.toString(“dd-MM-yyyy-HH.MM”)+“.JPG”}
您可以执行以下操作:
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
# start a loop to gather the files and reset their LastWriteTime property to the one read from the Exif data.
# pipe the result to the Sort-Object cmdlet and enter another ForEach-Object loop to perform the rename.
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$date = $_ | Get-ExifDate
if ($date) {
$_.LastWriteTime = $date
}
$_
} | Sort-Object LastWriteTime | ForEach-Object {
$newName = '{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm"))
# output some info to the console
Write-Host "Renaming file '$($_.Name)' to '$newName'"
$_ | Rename-Item -NewName $newName
}
编辑1
要按时间顺序命名它们,只需在脚本中添加一个
排序对象
,如下所示:
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
# start a loop to gather the files and reset their LastWriteTime property to the one read from the Exif data.
# pipe the result to the Sort-Object cmdlet and enter another ForEach-Object loop to perform the rename.
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$date = $_ | Get-ExifDate
if ($date) {
$_.LastWriteTime = $date
}
$_
} | Sort-Object LastWriteTime | ForEach-Object {
$newName = '{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm"))
# output some info to the console
Write-Host "Renaming file '$($_.Name)' to '$newName'"
$_ | Rename-Item -NewName $newName
}
编辑2
根据您最后的评论,要从图像中的Exif数据中获取日期,如果可能,您需要一个函数从文件中获取
DateTimeOriginal
您可以使用以下代码执行此操作:
function Get-ExifDate {
# returns the 'DateTimeOriginal' property from the Exif metadata in an image file if possible
[CmdletBinding(DefaultParameterSetName = 'ByName')]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'ByName')]
[Alias('FullName', 'FileName')]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf})]
[string]$Path,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = 'ByObject')]
[System.IO.FileInfo]$FileObject
)
Begin {
Add-Type -AssemblyName 'System.Drawing'
}
Process {
# the function received a path, not a file object
if ($PSCmdlet.ParameterSetName -eq 'ByName') {
$FileObject = Get-Item -Path $Path -Force -ErrorAction SilentlyContinue
}
# Parameters for FileStream: Open/Read/SequentialScan
$streamArgs = @(
$FileObject.FullName
[System.IO.FileMode]::Open
[System.IO.FileAccess]::Read
[System.IO.FileShare]::Read
1024, # Buffer size
[System.IO.FileOptions]::SequentialScan
)
try {
$stream = New-Object System.IO.FileStream -ArgumentList $streamArgs
$metaData = [System.Drawing.Imaging.Metafile]::FromStream($stream)
# get the 'DateTimeOriginal' property (ID = 36867) from the metadata
# Tag Dec TagId Hex TagName Writable Group Notes
# ------- --------- ------- -------- ----- -----
# 36867 0x9003 DateTimeOriginal string ExifIFD (date/time when original image was taken)
# see: https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
# get the date taken as an array of bytes
$exifDateBytes = $metaData.GetPropertyItem(36867).Value
# transform to string, but beware that this string is Null terminated, so cut off the trailing 0 character
$exifDateString = [System.Text.Encoding]::ASCII.GetString($exifDateBytes).TrimEnd("`0")
# return the parsed date
return [datetime]::ParseExact($exifDateString, "yyyy:MM:dd HH:mm:ss", $null)
}
catch{
Write-Warning -Message "Could not read Exif data from '$($FileObject.FullName)'"
}
finally {
If ($metaData) {$metaData.Dispose()}
If ($stream) {$stream.Close()}
}
}
}
使用该函数,您的代码将如下所示:
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
# start a loop to gather the files and reset their LastWriteTime property to the one read from the Exif data.
# pipe the result to the Sort-Object cmdlet and enter another ForEach-Object loop to perform the rename.
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$date = $_ | Get-ExifDate
if ($date) {
$_.LastWriteTime = $date
}
$_
} | Sort-Object LastWriteTime | ForEach-Object {
$newName = '{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm"))
# output some info to the console
Write-Host "Renaming file '$($_.Name)' to '$newName'"
$_ | Rename-Item -NewName $newName
}
这使用字符串格式
-f
。给它一个模板字符串,在花括号之间有编号的占位符
第一个{0:00000}
是一种格式化数字的方法,在本例中,前面的零字符长度最多为5个字符
第二个{1}
被格式化的日期字符串替换
使用
++
语法,每次迭代时,$Count
变量都会增加。您可以这样做:
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
# start a loop to gather the files and reset their LastWriteTime property to the one read from the Exif data.
# pipe the result to the Sort-Object cmdlet and enter another ForEach-Object loop to perform the rename.
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$date = $_ | Get-ExifDate
if ($date) {
$_.LastWriteTime = $date
}
$_
} | Sort-Object LastWriteTime | ForEach-Object {
$newName = '{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm"))
# output some info to the console
Write-Host "Renaming file '$($_.Name)' to '$newName'"
$_ | Rename-Item -NewName $newName
}
编辑1
要按时间顺序命名它们,只需在脚本中添加一个
排序对象
,如下所示:
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
# start a loop to gather the files and reset their LastWriteTime property to the one read from the Exif data.
# pipe the result to the Sort-Object cmdlet and enter another ForEach-Object loop to perform the rename.
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$date = $_ | Get-ExifDate
if ($date) {
$_.LastWriteTime = $date
}
$_
} | Sort-Object LastWriteTime | ForEach-Object {
$newName = '{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm"))
# output some info to the console
Write-Host "Renaming file '$($_.Name)' to '$newName'"
$_ | Rename-Item -NewName $newName
}
编辑2
根据您最后的评论,要从图像中的Exif数据中获取日期,如果可能,您需要一个函数从文件中获取
DateTimeOriginal
您可以使用以下代码执行此操作:
function Get-ExifDate {
# returns the 'DateTimeOriginal' property from the Exif metadata in an image file if possible
[CmdletBinding(DefaultParameterSetName = 'ByName')]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'ByName')]
[Alias('FullName', 'FileName')]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf})]
[string]$Path,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = 'ByObject')]
[System.IO.FileInfo]$FileObject
)
Begin {
Add-Type -AssemblyName 'System.Drawing'
}
Process {
# the function received a path, not a file object
if ($PSCmdlet.ParameterSetName -eq 'ByName') {
$FileObject = Get-Item -Path $Path -Force -ErrorAction SilentlyContinue
}
# Parameters for FileStream: Open/Read/SequentialScan
$streamArgs = @(
$FileObject.FullName
[System.IO.FileMode]::Open
[System.IO.FileAccess]::Read
[System.IO.FileShare]::Read
1024, # Buffer size
[System.IO.FileOptions]::SequentialScan
)
try {
$stream = New-Object System.IO.FileStream -ArgumentList $streamArgs
$metaData = [System.Drawing.Imaging.Metafile]::FromStream($stream)
# get the 'DateTimeOriginal' property (ID = 36867) from the metadata
# Tag Dec TagId Hex TagName Writable Group Notes
# ------- --------- ------- -------- ----- -----
# 36867 0x9003 DateTimeOriginal string ExifIFD (date/time when original image was taken)
# see: https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
# get the date taken as an array of bytes
$exifDateBytes = $metaData.GetPropertyItem(36867).Value
# transform to string, but beware that this string is Null terminated, so cut off the trailing 0 character
$exifDateString = [System.Text.Encoding]::ASCII.GetString($exifDateBytes).TrimEnd("`0")
# return the parsed date
return [datetime]::ParseExact($exifDateString, "yyyy:MM:dd HH:mm:ss", $null)
}
catch{
Write-Warning -Message "Could not read Exif data from '$($FileObject.FullName)'"
}
finally {
If ($metaData) {$metaData.Dispose()}
If ($stream) {$stream.Close()}
}
}
}
使用该函数,您的代码将如下所示:
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime | ForEach-Object {
$_ | Rename-Item -NewName ('{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm")))
}
$Path = 'D:\' # the folder where the jpg files are
$Count = 10 # the starting number. gets increased for each file
# start a loop to gather the files and reset their LastWriteTime property to the one read from the Exif data.
# pipe the result to the Sort-Object cmdlet and enter another ForEach-Object loop to perform the rename.
Get-ChildItem -Path $Path -Filter '*.JPG' -File | ForEach-Object {
$date = $_ | Get-ExifDate
if ($date) {
$_.LastWriteTime = $date
}
$_
} | Sort-Object LastWriteTime | ForEach-Object {
$newName = '{0:00000} {1}.JPG' -f $Count++, ($_.LastWriteTime.toString("dd-MM-yyyy HH.mm"))
# output some info to the console
Write-Host "Renaming file '$($_.Name)' to '$newName'"
$_ | Rename-Item -NewName $newName
}
这使用字符串格式
-f
。给它一个模板字符串,在花括号之间有编号的占位符
第一个{0:00000}
是一种格式化数字的方法,在本例中,前面的零字符长度最多为5个字符
第二个{1}
被格式化的日期字符串替换
使用
++
语法,$Count
变量在每次迭代中都会增加。这是西奥的好答案(+1)的替代方法,无需使用ForEach对象as
重命名项
直接接受管道输入
这需要-NewName
参数的脚本块,而$count
需要是[ref]
(请参见mklement0中的内容) 允许在占位符中直接应用格式字符串
$Path = 'D:\' # the folder where the jpg files are
$Count = [ref] 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime |
Rename-Item -NewName {"{0:D5} {1:dd-MM-yyyy HH.mm}{2}" -f `
$Count.Value++,$_.LastWriteTime,$_.Extension} -whatif
如果输出看起来正常,则删除尾随的
-WhatIf
参数以替代西奥的正确答案(+1),而不使用ForEach对象as
重命名项
直接接受管道输入
这需要-NewName
参数的脚本块,而$count
需要是[ref]
(请参见mklement0中的内容) 允许在占位符中直接应用格式字符串
$Path = 'D:\' # the folder where the jpg files are
$Count = [ref] 10 # the starting number. gets increased for each file
Get-ChildItem -Path $Path -Filter '*.JPG' -File | Sort-Object LastWriteTime |
Rename-Item -NewName {"{0:D5} {1:dd-MM-yyyy HH.mm}{2}" -f `
$Count.Value++,$_.LastWriteTime,$_.Extension} -whatif
如果输出看起来正常,请删除尾部的
-WhatIf
参数如果多个文件具有相同的LastWriteTime,您的计划是什么?您是否有“计数”以便添加到新文件名中?如果没有,那么“计数”是用来做什么的?@lit:您可以在格式示例中看到。:)如果多个文件具有相同的LastWriteTime,您的计划是什么?您是否有“计数”以便添加到新文件名中?如果没有,那么“计数”是用来做什么的?@lit:您可以在格式示例中看到。:)简单有效,但你应该真正解释你做得更好的地方。不是每个人都熟悉字符串格式或++
方法。谢谢!但有一个问题:它们的顺序不尽相同。这里有一个例子:你知道我如何确保它能从最老的到最新的命名吗?@TheMadTechnician你说得对。我现在已经添加了一些解释。@607我已经添加了一个排序对象
,因此文件将从最旧编号到最新编号。您好,您的脚本非常有用,但我刚刚意识到,在某些照片集上,修改日期与拍摄照片时的日期相差一小时。我发现了日期元数据,我应该使用它。我已经研究了获取此属性的方法,但恐怕我对Powershell命令还没有足够的经验来了解如何在脚本中实现它。你能帮我吗?简单有效,但你应该真正解释一下你做得更好的地方。不是每个人都熟悉字符串格式或++
方法。谢谢!但有一个问题:它们的顺序不尽相同。这里有一个例子:你知道我如何确保它能从最老的到最新的命名吗?@TheMadTechnician你说得对。我现在已经添加了一些解释。@607我已经添加了一个排序对象
,因此文件将从最旧编号到最新编号。您好,您的脚本非常有用,但我刚刚意识到,在某些图片集上,修改日期从图片开始的时间起会推迟一个小时