-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSet-DateTaken-from-FileName.ps1
49 lines (41 loc) · 1.98 KB
/
Set-DateTaken-from-FileName.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
# Define the target directory
$directory = "E:\temp\pics\test"
# Define the pattern to extract the date from filenames
#$dateTimePattern = "(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})" # Example: 20210720_200601_1177536
$dateTimePattern = "(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})" # Example: 2021-07-20_20-06-01_1177536
#$dateTimePattern = "x" # Example: 79576746_590191381795832_5037634142968217600_n.jpg
# Loop through all JPG files in the directory
Get-ChildItem -Path $directory -Filter "*.jpg" -Recurse | ForEach-Object {
$file = $_.FullName
# Extract DateTimeOriginal field using ExifTool
$exifData = & .\exiftool.exe -DateTimeOriginal -s3 $file
# Check if DateTimeOriginal is missing or empty
if ([string]::IsNullOrWhiteSpace($exifData)) {
Write-Host "No DateTaken found for: $file"
# Extract date and time from the filename using the pattern
if ($file -match $dateTimePattern) {
$year = $Matches[1]
$month = $Matches[2]
$day = $Matches[3]
$hour = $Matches[4]
$minute = $Matches[5]
$second = $Matches[6]
# Format the extracted date and time for EXIF
$formattedDate = "${year}:${month}:${day} ${hour}:${minute}:${second}"
Write-Host "Filename match: Using formatted date $formattedDate"
} else {
# Fall back to file's last modified time
$lastModified = (Get-Item $file).LastWriteTime
$formattedDate = $lastModified.ToString("yyyy:MM:dd HH:mm:ss")
Write-Host "No filename match: Using last modified time $formattedDate"
}
# Set the DateTaken EXIF data
& .\exiftool.exe -overwrite_original `
"-DateTimeOriginal=$formattedDate" `
"-CreateDate=$formattedDate" `
"-ModifyDate=$formattedDate" $file
Write-Host "Updated DateTaken for: $file to $formattedDate"
} else {
Write-Host "DateTaken already exists for: $file"
}
}