根据照片的拍摄时间重新命名照片文件名

这几天一直想给照片搞个备份,在寻找一个性价比比较高而且又比较稳定的方案,现在还没找到,先进行图片整理工作。
整理过程中想把所有的文件名给改一下,于是总结了这个脚本。
需要提前从这里https://exiftool.org/ 下载一个exiftool.exe用于操作图片或者视频的exif信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

$currentDirectory = Get-Location
$imageFiles = Get-ChildItem -Path $currentDirectory -File | Where-Object { $_.Extension -match '\.(jpg|jpeg|png|gif)' }

foreach ($imageFile in $imageFiles) {
$dateTaken = & "D:\exiftool.exe" "-DateTimeOriginal" "-d" "%Y%m%d_%H%M%S" "-s" $imageFile.FullName
$dateTaken = $dateTaken.Trim() -replace ".*: ", ""
if ($dateTaken -ne "") {
$newFileName = "IMG_$dateTaken$($imageFile.Extension)"
$newFilePath = Join-Path -Path $currentDirectory -ChildPath $newFileName
while (Test-Path $newFilePath) {
# 将日期时间字符串解析为日期时间对象
$year = [int]($dateTaken.Substring(0, 4))
$month = [int]($dateTaken.Substring(4, 2))
$day = [int]($dateTaken.Substring(6, 2))
$hour = [int]($dateTaken.Substring(9, 2))
$minute = [int]($dateTaken.Substring(11, 2))
$second = [int]($dateTaken.Substring(13, 2))
$newDate = Get-Date -Year $year -Month $month -Day $day -Hour $hour -Minute $minute -Second $second

# 递增一秒钟
$newDate = $newDate.AddSeconds(1)
$dateTaken = $newDate.ToString("yyyyMMdd_HHmmss")

$newFileName = "IMG_$dateTaken$($imageFile.Extension)"
$newFilePath = Join-Path -Path $currentDirectory -ChildPath $newFileName
}
Rename-Item -Path $imageFile.FullName -NewName $newFilePath -Force
}
Write-Host $imageFile
}

$videoFiles = Get-ChildItem -Path $currentDirectory -File | Where-Object { $_.Extension -match '\.(mp4|mov|avi|mkv)' }

foreach ($videoFile in $videoFiles) {
$mediaCreateTime = & "D:\exiftool.exe" "-MediaCreateDate" "-d" "%Y%m%d_%H%M%S" "-s" $videoFile.FullName
$mediaCreateTime = $mediaCreateTime.Trim() -replace ".*: ", ""

if ($mediaCreateTime -ne "") {
$newFileName = "VID_$mediaCreateTime$($videoFile.Extension)"
$newFilePath = Join-Path -Path $currentDirectory -ChildPath $newFileName

if ($videoFile.Name -ne $newFileName) {
Rename-Item -Path $videoFile.FullName -NewName $newFilePath -Force
}
}
Write-Host $videoFile
}

Write-Host "重命名完成。"

Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能要想想办法才能看到。

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2024 Tiaobug All Rights Reserved.

本站总访问量    次