ソースを参照

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

original creation date. Useful in case of data from
google
master
Figg 3ヶ月前
コミット
054eabc9f4
1個のファイルの変更77行の追加48行の削除
  1. 77
    48
      ArrangePictures.ps1

+ 77
- 48
ArrangePictures.ps1 ファイルの表示

@@ -1,4 +1,16 @@
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 14
     param (
3 15
         [string] $sourceUrl = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.52.0/db.json"
4 16
     )
@@ -15,7 +27,7 @@
15 27
     return $extensions
16 28
 }
17 29
 
18
-function Get-FileMetaData {
30
+function Get-APFileMetaData {
19 31
     param (
20 32
         [string] $filePath,
21 33
         [int] $propertyIndex
@@ -24,7 +36,7 @@ function Get-FileMetaData {
24 36
     $folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path -Parent $filePath))
25 37
     $folderItem = $folder.ParseName((Split-Path -Leaf $filePath))
26 38
 
27
-    $property = $Folder.GetDetailsOf(
39
+    $property = $folder.GetDetailsOf(
28 40
         $folderItem,
29 41
         $propertyIndex
30 42
     )
@@ -32,37 +44,50 @@ function Get-FileMetaData {
32 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 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 86
     param (
61
-        [string] $filePath
87
+        [string] $filePath,
88
+        [Nullable[datetime]] $originalDate
62 89
     )
63 90
 
64
-    $originalDate = Get-APFileDateTaken -FilePath $filePath
65
-
66 91
     if ($originalDate) {
67 92
         $year = $originalDate.Year
68 93
         $month = $originalDate.Month.ToString("D2")
@@ -74,50 +99,54 @@ function Get-APCreationDateSubDirectory {
74 99
     }
75 100
 }
76 101
 
77
-
78 102
 ###########################################
79 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 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 117
     try {
94
-        Expand-Archive -Path $SourceDir -DestinationPath $TempDir
95
-        $SourceDir = $TempDir
118
+        Expand-Archive -LiteralPath $sourceDir -DestinationPath $tempDir
119
+        $sourceDir = $tempDir
96 120
     }catch{
97 121
         Write-Host "L'archive n'a pas pu être décompressée."
98 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 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
 }

読み込み中…
キャンセル
保存