-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-TimestampByExifOrFilename.ps1
executable file
·165 lines (147 loc) · 4.59 KB
/
Set-TimestampByExifOrFilename.ps1
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<#
.SYNOPSIS
Change modification/creation time of image/video files by exif date or filename format
.DESCRIPTION
Change modification/creation time of image/video files by exif date or filename format
EXIF:
image: use tag 'ExifDTOrig' (id 36868) or 'ExifDTDigitized' (id 36867)
File name formats:
IMG[_-]YYYYMMDD[_-]HHmmss.*$ Generic image file
VID[_-]YYYYMMDD[_-]HHmmss.*$ Generic video file
SAVE[_-]YYYYMMDD[_-]HHmmss.*$ Generic image/video file saved
WhatsApp:
IMG-YYYYMMDD-WASEQ.*$ Generic "WhatsApp Image" file
VID-YYYYMMDD-WASEQ.*$ Generic "WhatsApp Video" file
AUD-YYYYMMDD-WASEQ.*$ Generic "WhatsApp Audio" file
STK-YYYYMMDD-WASEQ.*$ Generic "WhatsApp Stickers" file
PTT-YYYYMMDD-WASEQ.*$ Generic "WhatsApp Voice Notes" file
.PARAMETER File
Specifies the file or directory name
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)] [string]$File
)
<#
.DESCRIPTION
Change file modification/creation date by filename
- filename start with 3 to 5 letters;
- follow by a char '_' or '-';
- follow by 8 numbers from '0' to '9' (YYYYMMDD);
- follow by a char '_' or '-';
- follow by 2 numbers (hours) or WA for WhatsApp file;
- follow by 2 numbers (minutes);
- follow by 2 numbers (seconds).
If WhatsApp, WASEQ is tranlated from seq (WA0000 - WA9999) to hhmmss (000000 - 024639)
.PARAMETER File
Specifies the file name
.OUTPUTS
System.String. Set-TimestampByFilename returns a string "OK" if time changed else "ERR"
#>
function Set-TimestampByFilename() {
[CmdletBinding()]
param (
[Parameter(Mandatory)] [string]$File
)
if (! (Test-Path $File)) {
return "ERR: ${File}: not exist"
}
$fileName=$(Get-Item $File).Name
if (! ($fileName -match '^[A-Za-z]{3,5}[_-](\d{4})(\d{2})(\d{2})[_-](WA|\d{2})(\d{2})(\d{2}).*$')) {
return "ERR: Invalid file name format: ${fileName}"
}
$filedate=$Matches[1] + '/' + $Matches[2] +'/'+ $Matches[3] +' '+ $Matches[4] +':'+ $Matches[5] +':'+ $Matches[6]
if ($filedate -match 'WA:(?<mm>\d\d):(?<ss>\d\d)') {
$seq_wa=[int]($matches['mm'] + $matches['ss'])
$hh=[int]($seq_wa / 3600)
$mm=[Math]::Floor(($seq_wa - ($hh * 3600)) / 60)
$ss=[Math]::Floor($seq_wa - ($hh * 3600) - ($mm * 60) )
$hhmmss="{0:d2}:{1:d2}:{2:d2}" -f [int]$hh, [int]$mm, [int]$ss
$filedate = $filedate -replace 'WA:\d{2}:\d{2}', $hhmmss
}
$(Get-Item $File).creationtime=$(Get-Date $filedate)
$(Get-Item $File).lastwritetime=$(Get-Date $filedate)
Write-Host "${File}: setting date to ${filedate}"
return "OK"
}
<#
.D$filedateESCRIPTION
Change file modification/creation date by Exif
.PARAMETER File
Specifies the file name
.OUTPUTS
System.String. Set-TimestampByExif returns a string "OK" if time changed else "ERR"
#>
function Set-TimestampByExif() {
[CmdletBinding()]
param (
[Parameter(Mandatory)] [string]$File
)
if (! (Test-Path $File)) {
return "ERR: ${File}: not exist"
}
$fullName=$(Get-Item $File).FullName
$image = New-Object -ComObject Wia.ImageFile
try {
$image.LoadFile($fullName);
}
catch {
return "ERR: ${File}: not an image file"
}
if ($image.Properties.Length -eq 0) {
return "ERR: ${File}: not an image file"
}
if ( $image.Properties.Exists('ExifDTOrig')) {
$filedate=$image.Properties.Item('ExifDTOrig').Value
}
elseif ($image.Properties.Exists('ExifDTDigitized')) {
$filedate=$image.Properties.Exists('ExifDTDigitized').Value
}
else {
return "ERR: ${File}: Exif date not found"
}
if ($filedate -match '^\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d$') {
$filedate_obj=[datetime]::ParseExact($filedate,'yyyy:MM:dd HH:mm:ss',$null)
}
else {
return "ERR: ${File}: Exif date unrecognized format ($filedate)"
}
$(Get-Item $File).creationtime=$filedate_obj
$(Get-Item $File).lastwritetime=$filedate_obj
Write-Host "${File}: setting exif date to ${filedate}"
return "OK"
}
<#
.DESCRIPTION
Wrapper function for Set-TimestampByExif and Set-TimestampByFilename
.PARAMETER File
Specifies the file name
.OUTPUTS
System.String. returns a string "OK" if time changed else "ERR"
#>
function Set-Timestamp() {
[CmdletBinding()]
param (
[Parameter(Mandatory)] [string]$File
)
$ret=Set-TimestampByExif -File $File
if ($ret -match '^ERR') {
$ret2=Set-TimestampByFilename -File $File
if ($ret2 -match '^ERR') {
Write-Host "$ret, $ret2"
}
}
}
if ( Test-Path -PathType leaf $File) {
$fix=Set-Timestamp -File $File
}
elseif ( Test-Path -PathType Container $File) {
Get-ChildItem $File | ForEach-Object {
if (Test-Path -PathType leaf $_.Fullname) {
$fix=Set-Timestamp -File $_.Fullname
}
}
}
else {
Write-Host "ERR: ${File}: bad file/dir path"
}