Browse Source

Now uses .Json metadata file if it finds one to get

original creation date. Useful in case of data from
google
master
Figg 1 month ago
parent
commit
054eabc9f4
1 changed files with 77 additions and 48 deletions
  1. 77
    48
      ArrangePictures.ps1

+ 77
- 48
ArrangePictures.ps1 View File

1
-function Get-APImageVideoExtensions {
1
+##################################
2
+######     CONSTANTS     #########
3
+##################################
4
+
5
+$global:FileTypePropIndex = 11
6
+$global:ImageCreationDatePropIndex = 12
7
+$global:VideoCreationDatePropIndex = 208
8
+
9
+##################################
10
+######     FUNCTIONS     #########
11
+##################################
12
+
13
+function Get-APImageVideoExtensions {
2
     param (
14
     param (
3
         [string] $sourceUrl = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.52.0/db.json"
15
         [string] $sourceUrl = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.52.0/db.json"
4
     )
16
     )
15
     return $extensions
27
     return $extensions
16
 }
28
 }
17
 
29
 
18
-function Get-FileMetaData {
30
+function Get-APFileMetaData {
19
     param (
31
     param (
20
         [string] $filePath,
32
         [string] $filePath,
21
         [int] $propertyIndex
33
         [int] $propertyIndex
24
     $folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path -Parent $filePath))
36
     $folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path -Parent $filePath))
25
     $folderItem = $folder.ParseName((Split-Path -Leaf $filePath))
37
     $folderItem = $folder.ParseName((Split-Path -Leaf $filePath))
26
 
38
 
27
-    $property = $Folder.GetDetailsOf(
39
+    $property = $folder.GetDetailsOf(
28
         $folderItem,
40
         $folderItem,
29
         $propertyIndex
41
         $propertyIndex
30
     )
42
     )
32
     return $property
44
     return $property
33
 }
45
 }
34
 
46
 
35
-function Get-APFileDateTaken {
47
+function Get-APTemporaryDirectory {
48
+    do {
49
+        $tempDir = Join-Path -Path $env:TEMP -ChildPath ([System.IO.Path]::GetRandomFileName())
50
+    } while (Test-Path $tempDir)
51
+    return $tempDir
52
+}
53
+
54
+function Get-APDateFromJson {
36
     param (
55
     param (
37
-        [string] $FilePath
56
+        [string] $filePath
38
     )
57
     )
39
-    #$PropIndex = 208  # Image Date Taken Property Index ; 208 = vidéo ; 11 = File type
40
-    $CharWhiteList = '[^: \w\/]'
41
-    $type = (Get-FileMetaData -FilePath $FilePath -PropertyIndex 11)
42
-    if ($type -ieq "vidéo") { $PropIndex = 208 }
43
-    if ($type -ieq "image") { $PropIndex = 12 }
44
 
58
 
45
-    $DateTaken = (Get-FileMetaData -FilePath $FilePath -PropertyIndex $PropIndex) -replace $CharWhiteList
59
+    $jsonFilePath = $filePath + ".json"
60
+    if (-not (Test-Path $jsonFilePath)) {
61
+        return $null
62
+    }
46
 
63
 
47
-    $culture = Get-Culture
48
-    $DateTaken = try { [DateTime]::Parse($DateTaken, $culture) } catch { $null }
49
-    return $DateTaken
64
+    $jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json
65
+    $timestamp = [int64] $jsonContent.photoTakenTime.timestamp
66
+    return [System.DateTimeOffset]::FromUnixTimeSeconds($timestamp).DateTime
50
 }
67
 }
51
 
68
 
52
-function Get-APTemporaryDirectory {
53
-    do {
54
-        $TempDir = Join-Path -Path $env:TEMP -ChildPath ([System.IO.Path]::GetRandomFileName())
55
-    } while (Test-Path $TempDir)
56
-    return $TempDir
69
+function Get-APFileCreationDate {
70
+    param (
71
+        [string] $filePath,
72
+        [string] $fileType
73
+    )
74
+    if ($fileType -ieq "vidéo") { $propIndex = $VideoCreationDatePropIndex }
75
+    if ($fileType -ieq "image") { $propIndex = $ImageCreationDatePropIndex }
76
+    $charWhiteList = '[^: \w\/]'
77
+
78
+    $dateTaken = (Get-APFileMetaData -FilePath $filePath -PropertyIndex $propIndex) -replace $charWhiteList
79
+
80
+    $culture = Get-Culture
81
+    $dateTaken = try { [DateTime]::Parse($dateTaken, $culture) } catch { $null }
82
+    return $dateTaken
57
 }
83
 }
58
 
84
 
59
-function Get-APCreationDateSubDirectory {
85
+function _subDirectoryFromDate {
60
     param (
86
     param (
61
-        [string] $filePath
87
+        [string] $filePath,
88
+        [Nullable[datetime]] $originalDate
62
     )
89
     )
63
 
90
 
64
-    $originalDate = Get-APFileDateTaken -FilePath $filePath
65
-
66
     if ($originalDate) {
91
     if ($originalDate) {
67
         $year = $originalDate.Year
92
         $year = $originalDate.Year
68
         $month = $originalDate.Month.ToString("D2")
93
         $month = $originalDate.Month.ToString("D2")
74
     }
99
     }
75
 }
100
 }
76
 
101
 
77
-
78
 ###########################################
102
 ###########################################
79
 ####              MAIN                #####
103
 ####              MAIN                #####
80
 ###########################################
104
 ###########################################
81
 
105
 
82
-if (-not $args[0] -or -not $args[1]) {
83
-    $SourceDir = Read-Host "Veuillez entrer le chemin du dossier source (ou une archive .zip)"
84
-    $DestinationDir = Read-Host "Veuillez entrer le chemin du dossier destination"
106
+if ($args[0] -and $args[1]) {
107
+    $sourceDir = $args[0]
108
+    $destinationDir = $args[1]
85
 } else {
109
 } else {
86
-    $SourceDir = $args[0]
87
-    $DestinationDir = $args[1]
110
+    $sourceDir = Read-Host "Veuillez entrer le chemin du dossier source (ou une archive .zip)"
111
+    $destinationDir = Read-Host "Veuillez entrer le chemin du dossier destination"
88
 }
112
 }
89
 
113
 
90
-if ($SourceDir -imatch "\.zip$") {
91
-    $TempDir = Get-APTemporaryDirectory
114
+if ($sourceDir -imatch "\.zip$") {
115
+    $tempDir = Get-APTemporaryDirectory
92
 
116
 
93
     try {
117
     try {
94
-        Expand-Archive -Path $SourceDir -DestinationPath $TempDir
95
-        $SourceDir = $TempDir
118
+        Expand-Archive -LiteralPath $sourceDir -DestinationPath $tempDir
119
+        $sourceDir = $tempDir
96
     }catch{
120
     }catch{
97
         Write-Host "L'archive n'a pas pu être décompressée."
121
         Write-Host "L'archive n'a pas pu être décompressée."
98
         return
122
         return
99
     }
123
     }
100
 }
124
 }
101
 
125
 
102
-$ValidExtensions = Get-APImageVideoExtensions
103
-Get-ChildItem -Path $SourceDir -Recurse -File | ForEach-Object {
104
-    $File = $_
105
-    $FileExtension = $File.Extension.ToLower().TrimStart('.')
126
+Get-ChildItem -Path $sourceDir -Recurse -File | ForEach-Object {
127
+    $filePath = $_.FullName
128
+    $fileType = (Get-APFileMetaData -File $filePath -PropertyIndex $FileTypePropIndex)
106
 
129
 
107
-    if ($ValidExtensions -contains $FileExtension) {
108
-        $subFolder = Get-APCreationDateSubDirectory -filePath $File.FullName
109
-        $targetDir = Join-Path -Path $DestinationDir -ChildPath $subFolder
110
-
111
-        if (-not (Test-Path $targetDir)) {
112
-            New-Item -Path $targetDir -ItemType Directory | Out-Null
113
-        }
130
+    if (-not($fileType -match "vidéo|image")) {
131
+        Write-Host "Fichier ignoré: $($filePath)"
132
+        return
133
+    }
114
 
134
 
115
-        Copy-Item -Path $file.FullName -Destination $targetDir
135
+    $creationDate = if ($value = Get-APDateFromJson -filePath $filePath) {
136
+        $value
116
     } else {
137
     } else {
117
-        Write-Host "Fichier ignoré: $($file.FullName)"
138
+        Get-APFileCreationDate -filePath $filePath -fileType $fileType
118
     }
139
     }
140
+    $subFolder = _subDirectoryFromDate -filePath $filePath -originalDate $creationDate
141
+    $targetDir = Join-Path -Path $DestinationDir -ChildPath $subFolder
142
+
143
+    if (-not (Test-Path $targetDir)) {
144
+        New-Item -Path $targetDir -ItemType Directory | Out-Null
145
+    }
146
+
147
+    Copy-Item -Path $filePath -Destination $targetDir
119
 }
148
 }
120
 
149
 
121
-if ($SourceDir -like "$env:TEMP\*") {
122
-    Remove-Item -Path $SourceDir -Recurse -Force
150
+if ($sourceDir -like "$env:TEMP\*") {
151
+    Remove-Item -Path $sourceDir -Recurse -Force
123
 }
152
 }

Loading…
Cancel
Save