Un script Powershell pour trier les photos
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ArrangePictures.ps1 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function Get-APImageVideoExtensions {
  2. param (
  3. [string] $sourceUrl = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.52.0/db.json"
  4. )
  5. $jsonData = (Invoke-RestMethod $sourceUrl).PSObject.Properties
  6. $extensions = @()
  7. foreach ($item in $jsonData) {
  8. if ($item.Name -match "^(image|video)") {
  9. $extensions += $item.Value.extensions
  10. }
  11. }
  12. $extensions = $extensions | Sort-Object -Unique
  13. return $extensions
  14. }
  15. function Get-FileMetaData {
  16. param (
  17. [string] $filePath,
  18. [int] $propertyIndex
  19. )
  20. $folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path -Parent $filePath))
  21. $folderItem = $folder.ParseName((Split-Path -Leaf $filePath))
  22. $property = $Folder.GetDetailsOf(
  23. $folderItem,
  24. $propertyIndex
  25. )
  26. return $property
  27. }
  28. function Get-APFileDateTaken {
  29. param (
  30. [string] $FilePath
  31. )
  32. #$PropIndex = 208 # Image Date Taken Property Index ; 208 = vidéo ; 11 = File type
  33. $CharWhiteList = '[^: \w\/]'
  34. $type = (Get-FileMetaData -FilePath $FilePath -PropertyIndex 11)
  35. if ($type -ieq "vidéo") { $PropIndex = 208 }
  36. if ($type -ieq "image") { $PropIndex = 12 }
  37. $DateTaken = (Get-FileMetaData -FilePath $FilePath -PropertyIndex $PropIndex) -replace $CharWhiteList
  38. $culture = Get-Culture
  39. $DateTaken = try { [DateTime]::Parse($DateTaken, $culture) } catch { $null }
  40. return $DateTaken
  41. }
  42. function Get-APTemporaryDirectory {
  43. do {
  44. $TempDir = Join-Path -Path $env:TEMP -ChildPath ([System.IO.Path]::GetRandomFileName())
  45. } while (Test-Path $TempDir)
  46. return $TempDir
  47. }
  48. function Get-APCreationDateSubDirectory {
  49. param (
  50. [string] $filePath
  51. )
  52. $originalDate = Get-APFileDateTaken -FilePath $filePath
  53. if ($originalDate) {
  54. $year = $originalDate.Year
  55. $month = $originalDate.Month.ToString("D2")
  56. $monthName = Get-Culture | ForEach-Object { $_.DateTimeFormat.GetMonthName($month) }
  57. return "$year\$month - $monthName"
  58. } else {
  59. return "Non daté"
  60. }
  61. }
  62. ###########################################
  63. #### MAIN #####
  64. ###########################################
  65. if (-not $args[0] -or -not $args[1]) {
  66. $SourceDir = Read-Host "Veuillez entrer le chemin du dossier source (ou une archive .zip)"
  67. $DestinationDir = Read-Host "Veuillez entrer le chemin du dossier destination"
  68. } else {
  69. $SourceDir = $args[0]
  70. $DestinationDir = $args[1]
  71. }
  72. if ($SourceDir -imatch "\.zip$") {
  73. $TempDir = Get-APTemporaryDirectory
  74. try {
  75. Expand-Archive -Path $SourceDir -DestinationPath $TempDir
  76. $SourceDir = $TempDir
  77. }catch{
  78. Write-Host "L'archive n'a pas pu être décompressée."
  79. return
  80. }
  81. }
  82. $ValidExtensions = Get-APImageVideoExtensions
  83. Get-ChildItem -Path $SourceDir -Recurse -File | ForEach-Object {
  84. $File = $_
  85. $FileExtension = $File.Extension.ToLower().TrimStart('.')
  86. if ($ValidExtensions -contains $FileExtension) {
  87. $subFolder = Get-APCreationDateSubDirectory -filePath $File.FullName
  88. $targetDir = Join-Path -Path $DestinationDir -ChildPath $subFolder
  89. if (-not (Test-Path $targetDir)) {
  90. New-Item -Path $targetDir -ItemType Directory | Out-Null
  91. }
  92. Copy-Item -Path $file.FullName -Destination $targetDir
  93. } else {
  94. Write-Host "Fichier ignoré: $($file.FullName)"
  95. }
  96. }
  97. if ($SourceDir -like "$env:TEMP\*") {
  98. Remove-Item -Path $SourceDir -Recurse -Force
  99. }