Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. <#
  2. .Synopsis
  3. Sorts picture files from camera sources into appropriate
  4. folders for filing.
  5. .DESCRIPTION
  6. Compares the set of pictures in the camera source folder
  7. and the backup folder, then merges/sorts the contents into
  8. a third location based on how they need to be filed.
  9.  
  10. Items get sorted into one of four groups:
  11. - To File: Items only in camera or items in camera and backup.
  12. - Only In Backup: Items only in backup.
  13. - Duplicate Camera Items: Items that appear multiple times in the camera folder.
  14. - Backups To Remove After Filing: Items that were in the backup folder where originals are ready to file.
  15. .EXAMPLE
  16. .\Sort-PictureFiles.ps1 -CameraFolder .\camera -BackupFolder .\backup -DestinationFolder .\destination
  17. #>
  18. [CmdletBinding()]
  19. Param(
  20. [Parameter(
  21. Mandatory=$True,
  22. HelpMessage="The folder where all the pictures and videos from cameras are located. All subfolders will be considered picture sources. THIS FOLDER WILL BE EMPTIED AFTER SORTING.")]
  23. [ValidateNotNullOrEmpty()]
  24. [string]
  25. $CameraFolder,
  26.  
  27. [Parameter(
  28. Mandatory=$True,
  29. HelpMessage="The folder where pictures are automatically backed up. All subfolders will be considered picture sources. THIS FOLDER WILL BE EMPTIED AFTER SORTING.")]
  30. [ValidateNotNullOrEmpty()]
  31. [string]
  32. $BackupFolder,
  33.  
  34. [Parameter(
  35. Mandatory=$True,
  36. HelpMessage="The folder where sorted pictures and videos should be placed. All camera and backup pictures will be sorted into folders in here.")]
  37. [ValidateNotNullOrEmpty()]
  38. [string]
  39. $DestinationFolder,
  40.  
  41. [Parameter(
  42. Mandatory=$False,
  43. HelpMessage="The folder where sorted pictures and videos should be placed. All camera and backup pictures will be sorted into folders in here.")]
  44. [ValidateNotNullOrEmpty()]
  45. [string]
  46. $ExifTool = "exiftool.exe"
  47. )
  48. Begin
  49. {
  50. If (-Not (Test-Path $CameraFolder -PathType Container))
  51. {
  52. throw "The camera folder $CameraFolder does not exist."
  53. }
  54. If (-Not (Test-Path $BackupFolder -PathType Container))
  55. {
  56. throw "The backup folder $BackupFolder does not exist."
  57. }
  58. If (-Not (Test-Path $DestinationFolder -PathType Container))
  59. {
  60. throw "The sort destination folder $DestinationFolder does not exist."
  61. }
  62. if ((Get-Command $ExifTool -ErrorAction SilentlyContinue) -eq $NULL)
  63. {
  64. Write-Host "Unable to find exiftool.exe in your path."
  65. }
  66.  
  67. $toFileFolder = New-Item -Path "$DestinationFolder" -Name "To File" -ItemType Directory -Force
  68. $onlyInBackupFolder = New-Item -Path "$DestinationFolder" -Name "Only In Backup" -ItemType Directory -Force
  69. $duplicateFolder = New-Item -Path "$DestinationFolder" -Name "Duplicate Camera Items" -ItemType Directory -Force
  70. $removeAfterFiling = New-Item -Path "$DestinationFolder" -Name "Backups To Remove After Filing" -ItemType Directory -Force
  71.  
  72. function SafeMove()
  73. {
  74. Param($SourceFile, $DestinationFolder)
  75. Write-Verbose "Moving $SourceFile"
  76. $name = [System.IO.Path]::GetFileNameWithoutExtension($SourceFile)
  77. $ext = [System.IO.Path]::GetExtension($SourceFile)
  78. if(-Not (Test-Path (Join-Path -Path $DestinationFolder -ChildPath "$name$ext")))
  79. {
  80. Move-Item -Force -Path $SourceFile -Destination $DestinationFolder
  81. return;
  82. }
  83.  
  84. $count = 0
  85. while(Test-Path (Join-Path -Path $DestinationFolder -ChildPath "$name-$count$ext"))
  86. {
  87. $count++
  88. }
  89.  
  90. Move-Item -Force -Path $SourceFile -Destination (Join-Path -Path $DestinationFolder -ChildPath "$name-$count$ext")
  91. }
  92. }
  93. Process
  94. {
  95. # Get pairs of hash/path for camera folder.
  96. $cameraHashes = Get-ChildItem -Path $CameraFolder -Recurse -File | Sort-Object -Property FullName | Get-FileHash
  97. Write-Verbose "Camera file hashes:"
  98. $cameraHashes | Write-Verbose
  99. $cameraMoved = @()
  100. for($i = 0; $i -lt $cameraHashes.length; $i++)
  101. {
  102. $current = $cameraHashes[$i];
  103. if($cameraMoved -contains $current.Hash)
  104. {
  105. # It's a duplicate in the camera folder (e.g., one person mailed a copy of a photo to another).
  106. SafeMove $current.Path $duplicateFolder.FullName
  107. }
  108. else
  109. {
  110. # It's a one-of-a-kind in the camera folder - file it!
  111. SafeMove $current.Path $toFileFolder.FullName
  112. $cameraMoved += $current.Hash
  113. }
  114. }
  115.  
  116. # Get pairs of hash/path for backup folder.
  117. $backupHashes = Get-ChildItem -Path $BackupFolder -Recurse -File | Sort-Object -Property FullName | Get-FileHash
  118. Write-Verbose "Backup file hashes:"
  119. $backupHashes | Write-Verbose
  120. $backupsMoved = @()
  121. for($i = 0; $i -lt $backupHashes.length; $i++)
  122. {
  123. $current = $backupHashes[$i];
  124. if($backupsMoved -contains $current.Hash)
  125. {
  126. # It's a duplicate backup - assume it can be removed after filing.
  127. SafeMove $current.Path $removeAfterFiling.FullName
  128. continue;
  129. }
  130.  
  131. $found = $cameraHashes | Where-Object { $_.Hash -eq $current.Hash }
  132. if ($found -ne $NULL)
  133. {
  134. # It's a backup of an item found in the camera folder - assume it can be removed after filing.
  135. SafeMove $current.Path $removeAfterFiling.FullName
  136. $backupsMoved += $current.Hash
  137. continue;
  138. }
  139.  
  140. # It wasn't a duplicate backup and it wasn't in the camera folder - possible recovery?
  141. SafeMove $current.Path $onlyInBackupFolder.FullName
  142. $backupsMoved += $current.Hash
  143. }
  144.  
  145. foreach($location in ($toFileFolder.FullName, $onlyInBackupFolder.FullName, $duplicateFolder.FullName, $removeAfterFiling.FullName))
  146. {
  147. Push-Location $location
  148. & $ExifTool "-FileName<DateTimeOriginal" -d "%Y%m%d_%H%M%S%%-c.%%e" *.jpg
  149. Pop-Location
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement