Advertisement
J2897

Camera Transfer

Apr 27th, 2016
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# Released under the GNU General Public License version 3+ by J2897.
  2.  
  3.     Filename:   camera_transfer.ps1
  4.     Version:    1.0
  5.     Latest:     https://pastebin.com/x7vsEctC
  6.     Contact:    https://pastebin.com/message_compose?to=J2897
  7.     Flowchart:  https://db.tt/JjZ2p8nW
  8.  
  9.     This is for transferring video or audio files from any camera, phone or other device, and generating a list of SHA1 hashes for verification purposes.
  10.    
  11.     On the first run, the files will be copied to the destination.
  12.    
  13.     On the second run, any new files will be copied to the destination, and any previously copied files you haven't yet deleted from the source will be verified against the files in the destination.
  14.    
  15.     Verification will take as long as copying since the whole file is checked against the SHA1 hash in the Hashes.csv file.
  16.  
  17. AMIABTY...
  18. All my ideas are belong to you. #>
  19.  
  20. # https://gallery.technet.microsoft.com/scriptcenter/Get-Hashes-of-Files-1d85de46
  21. . 'C:\Users\John\Code\PowerShell\Functions\Get-FileHash.ps1'
  22.  
  23. # Start.
  24. clear
  25. $SourceURI = 'I:\DCIM\100HDDVR'
  26. $DestinationURI = 'J:\Mobius\Backups'
  27. #<#
  28. $SourceURI = 'C:\Users\John\TEST\SOURCE'
  29. $DestinationURI = 'C:\Users\John\TEST\DEST'
  30. #>
  31. $HashesFileName = 'Hashes.csv'
  32. $HashesFileURI = "$DestinationURI\$HashesFileName"
  33. $Date = Get-Date
  34. $BeginTime = $Date.ToShortDateString() + ' ' + $Date.ToLongTimeString()
  35. Write-Host "At $BeginTime the backup began." -ForegroundColor Black -BackgroundColor White
  36.  
  37. # New data table function.
  38. function New-Table {
  39.     param([string]$TableName, [hashtable]$Columns)
  40.     $Table = New-Object system.Data.DataTable $TableName
  41.     foreach ($Column in $Columns.GetEnumerator()) {
  42.         $NewColumn = New-Object system.Data.DataColumn $($Column.Name),($($Column.Value))
  43.         $Table.columns.add($NewColumn)
  44.     }
  45.     return, $Table
  46. }
  47.  
  48. # Check for hash file.
  49. if (Test-Path $HashesFileURI) {
  50.     $HashesFile = Import-Csv "$HashesFileURI" -Encoding UTF8
  51. }
  52.  
  53. # Create SHA1 table.
  54. $SHA1Columns = @{
  55.     'SHA1' = [string];
  56.     'CreationDateTime' = [datetime];
  57.     'FileName' = [string]
  58. }
  59. $SHA1Table = New-Table -TableName 'SHA1Table' -Columns $SHA1Columns
  60.  
  61. # Create SkippedHashes table.
  62. $SkippedColumns = @{
  63.     'CreationDateTime' = [datetime];
  64.     'FileName' = [string]
  65. }
  66. $SkippedTable = New-Table -TableName 'SkippedHashes' -Columns $SkippedColumns
  67.  
  68. # Copy function.
  69. function Copy-File {
  70.     Write-Host "Copying:  $FileURI" -ForegroundColor Green
  71.     Copy-Item $SourceURI\$FileURI -Destination $DestinationURI\$CreationTimeString$Extension
  72.     $Hash = Get-FileHash $DestinationURI\$CreationTimeString$Extension -Algorithm SHA1 | Select-Object -ExpandProperty SHA1
  73.     $NewRow = $SHA1Table.NewRow()
  74.     $NewRow.SHA1 = $Hash
  75.     $NewRow.CreationDateTime = $CreationTime
  76.     $NewRow.FileName = $CreationTimeString + $Extension
  77.     $SHA1Table.Rows.Add($NewRow)
  78. }
  79.  
  80. #<#
  81. # Skip function.
  82. function Skip-File {
  83.     #Write-Host "Skipping: $FileURI" -ForegroundColor Yellow
  84.     $NewRow = $SkippedTable.NewRow()
  85.     #$NewRow.SHA1 = $Hash
  86.     $NewRow.CreationDateTime = $CreationTime
  87.     $NewRow.FileName = $CreationTimeString + $Extension
  88.     $SkippedTable.Rows.Add($NewRow)
  89. }
  90. #>
  91.  
  92. # Generate the hashes and copy the files.
  93. foreach ($FileURI in Get-ChildItem $SourceURI) {
  94.     $CreationTime = Get-Item $SourceURI\$FileURI | Select-Object -ExpandProperty CreationTime
  95.     $CreationTimeString = $CreationTime | Get-Date -format 'yyyy-MM-dd_HH.mm.ss'
  96.     $Extension = $FileURI | Select-Object -ExpandProperty Extension
  97.     if (Test-Path $DestinationURI\$CreationTimeString$Extension) {
  98.         if ((Get-Item $DestinationURI\$CreationTimeString$Extension).length -eq (Get-Item $SourceURI\$FileURI).length) {
  99.             Write-Host "Already exists:`t`t$CreationTimeString$Extension = $FileURI"
  100.             if (Test-Path variable:HashesFile) {
  101.                 $Hash = Get-FileHash $SourceURI\$FileURI -Algorithm SHA1 | Select-Object -ExpandProperty SHA1
  102.                 if ($Hash -in $HashesFile.SHA1) {Skip-File} else {Write-Host "Mismatch: $Hash $FileURI"  -ForegroundColor Yellow -BackgroundColor Red}
  103.             } else {Skip-File}
  104.         } else {
  105.             Write-Host "`aSize difference:`t$CreationTimeString$Extension ≠ $FileURI" -ForegroundColor Yellow -BackgroundColor Red
  106.             Skip-File
  107.         }
  108.     } else {Copy-File}
  109. }
  110.  
  111. <#
  112. # Verify hashes of the copied files and delete the verified source files.
  113. if (Test-Path variable:HashesFile) {
  114.     foreach ($FileURI in Get-ChildItem $DestinationURI | Where-Object {$_.Name -NotMatch $HashesFileName}) {
  115.         $Hash = Get-FileHash $DestinationURI\$FileURI -Algorithm SHA1 | Select-Object -ExpandProperty SHA1
  116.         if ($Hash -in $HashesFile.SHA1) {
  117.             Write-Host "Verified: $Hash $FileURI" -ForegroundColor DarkGreen
  118.         } else {
  119.             Write-Host "Mismatch: $Hash $FileURI" -ForegroundColor Red
  120.         }
  121.     }
  122. }
  123. #>
  124.  
  125. # Dump table to CSV.
  126. $SHA1Table | Sort-Object DateTime | Export-Csv "$HashesFileURI" -Encoding UTF8 -Append
  127.  
  128. <#
  129. Write-Host 'Table: SHA1' -ForegroundColor DarkGreen
  130. $SHA1Table | format-table -AutoSize
  131.  
  132. Write-Host 'Table: HashesFile' -ForegroundColor DarkGreen
  133. $HashesFile | format-table -AutoSize
  134.  
  135. Write-Host 'Table: SkippedHashes' -ForegroundColor DarkGreen
  136. $SkippedTable | format-table -AutoSize
  137. #>
  138.  
  139. # End.
  140. $Date = Get-Date
  141. $EndTime = $Date.ToShortDateString() + ' ' + $Date.ToLongTimeString()
  142. Write-Host "At $EndTime the backup ended." -ForegroundColor Black -BackgroundColor White
  143.  
  144. #<#
  145. Write-Host 'Press any key to end...'
  146. [void][System.Console]::ReadKey($true)
  147. #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement