Un script Powershell pour trier les photos
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrangePictures.ps1 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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-APFileDateTaken {
  16. param (
  17. [string] $FilePath
  18. )
  19. $Folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path $FilePath))
  20. $PropIndex = 12 # Date Taken Property Index
  21. $CharWhiteList = '[^: \w\/]'
  22. $DateTaken = $Folder.GetDetailsOf(
  23. $Folder.ParseName((Split-Path -Leaf $FilePath)),
  24. $PropIndex
  25. ) -replace $CharWhiteList
  26. $culture = Get-Culture
  27. $DateTaken = try { [DateTime]::Parse($DateTaken, $culture) } catch { $null }
  28. return $DateTaken
  29. }
  30. function Get-APTemporaryDirectory {
  31. do {
  32. $TempDir = Join-Path -Path $env:TEMP -ChildPath ([System.IO.Path]::GetRandomFileName())
  33. } while (Test-Path $TempDir)
  34. return $TempDir
  35. }
  36. function Get-APCreationDateSubDirectory {
  37. param (
  38. [string] $filePath
  39. )
  40. $originalDate = Get-APFileDateTaken -FilePath $filePath
  41. if ($originalDate) {
  42. $year = $originalDate.Year
  43. $month = $originalDate.Month.ToString("D2")
  44. $monthName = Get-Culture | ForEach-Object { $_.DateTimeFormat.GetMonthName($month) }
  45. return "$year\$month - $monthName"
  46. } else {
  47. return "Non daté"
  48. }
  49. }
  50. if (-not $args[0] -or -not $args[1]) {
  51. $SourceDir = Read-Host "Veuillez entrer le chemin du dossier source (ou une archive .zip)"
  52. $DestinationDir = Read-Host "Veuillez entrer le chemin du dossier destination"
  53. } else {
  54. $SourceDir = $args[0]
  55. $DestinationDir = $args[1]
  56. }
  57. if ($SourceDir -imatch "\.zip$") {
  58. $TempDir = Get-APTemporaryDirectory
  59. try {
  60. Expand-Archive -Path $SourceDir -DestinationPath $TempDir
  61. $SourceDir = $TempDir
  62. }catch{
  63. Write-Host "L'archive n'a pas pu être décompressée."
  64. return
  65. }
  66. }
  67. $ValidExtensions = Get-APImageVideoExtensions
  68. Get-ChildItem -Path $SourceDir -Recurse -File | ForEach-Object {
  69. $File = $_
  70. $FileExtension = $File.Extension.ToLower().TrimStart('.')
  71. if ($ValidExtensions -contains $FileExtension) {
  72. $subFolder = Get-APCreationDateSubDirectory -filePath $File.FullName
  73. $targetDir = Join-Path -Path $DestinationDir -ChildPath $subFolder
  74. if (-not (Test-Path $targetDir)) {
  75. New-Item -Path $targetDir -ItemType Directory | Out-Null
  76. }
  77. Copy-Item -Path $file.FullName -Destination $targetDir
  78. } else {
  79. Write-Host "Fichier ignoré: $($file.FullName)"
  80. }
  81. }
  82. if ($SourceDir -like "$env:TEMP\*") {
  83. Remove-Item -Path $SourceDir -Recurse -Force
  84. }