Un script Powershell pour trier les photos
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ArrangePictures.ps1 2.8KB

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