Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# Released under the GNU General Public License version 3+ by J2897.
- Filename: camera_transfer.ps1
- Version: 1.0
- Latest: https://pastebin.com/x7vsEctC
- Contact: https://pastebin.com/message_compose?to=J2897
- Flowchart: https://db.tt/JjZ2p8nW
- 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.
- On the first run, the files will be copied to the destination.
- 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.
- Verification will take as long as copying since the whole file is checked against the SHA1 hash in the Hashes.csv file.
- AMIABTY...
- All my ideas are belong to you. #>
- # https://gallery.technet.microsoft.com/scriptcenter/Get-Hashes-of-Files-1d85de46
- . 'C:\Users\John\Code\PowerShell\Functions\Get-FileHash.ps1'
- # Start.
- clear
- $SourceURI = 'I:\DCIM\100HDDVR'
- $DestinationURI = 'J:\Mobius\Backups'
- #<#
- $SourceURI = 'C:\Users\John\TEST\SOURCE'
- $DestinationURI = 'C:\Users\John\TEST\DEST'
- #>
- $HashesFileName = 'Hashes.csv'
- $HashesFileURI = "$DestinationURI\$HashesFileName"
- $Date = Get-Date
- $BeginTime = $Date.ToShortDateString() + ' ' + $Date.ToLongTimeString()
- Write-Host "At $BeginTime the backup began." -ForegroundColor Black -BackgroundColor White
- # New data table function.
- function New-Table {
- param([string]$TableName, [hashtable]$Columns)
- $Table = New-Object system.Data.DataTable $TableName
- foreach ($Column in $Columns.GetEnumerator()) {
- $NewColumn = New-Object system.Data.DataColumn $($Column.Name),($($Column.Value))
- $Table.columns.add($NewColumn)
- }
- return, $Table
- }
- # Check for hash file.
- if (Test-Path $HashesFileURI) {
- $HashesFile = Import-Csv "$HashesFileURI" -Encoding UTF8
- }
- # Create SHA1 table.
- $SHA1Columns = @{
- 'SHA1' = [string];
- 'CreationDateTime' = [datetime];
- 'FileName' = [string]
- }
- $SHA1Table = New-Table -TableName 'SHA1Table' -Columns $SHA1Columns
- # Create SkippedHashes table.
- $SkippedColumns = @{
- 'CreationDateTime' = [datetime];
- 'FileName' = [string]
- }
- $SkippedTable = New-Table -TableName 'SkippedHashes' -Columns $SkippedColumns
- # Copy function.
- function Copy-File {
- Write-Host "Copying: $FileURI" -ForegroundColor Green
- Copy-Item $SourceURI\$FileURI -Destination $DestinationURI\$CreationTimeString$Extension
- $Hash = Get-FileHash $DestinationURI\$CreationTimeString$Extension -Algorithm SHA1 | Select-Object -ExpandProperty SHA1
- $NewRow = $SHA1Table.NewRow()
- $NewRow.SHA1 = $Hash
- $NewRow.CreationDateTime = $CreationTime
- $NewRow.FileName = $CreationTimeString + $Extension
- $SHA1Table.Rows.Add($NewRow)
- }
- #<#
- # Skip function.
- function Skip-File {
- #Write-Host "Skipping: $FileURI" -ForegroundColor Yellow
- $NewRow = $SkippedTable.NewRow()
- #$NewRow.SHA1 = $Hash
- $NewRow.CreationDateTime = $CreationTime
- $NewRow.FileName = $CreationTimeString + $Extension
- $SkippedTable.Rows.Add($NewRow)
- }
- #>
- # Generate the hashes and copy the files.
- foreach ($FileURI in Get-ChildItem $SourceURI) {
- $CreationTime = Get-Item $SourceURI\$FileURI | Select-Object -ExpandProperty CreationTime
- $CreationTimeString = $CreationTime | Get-Date -format 'yyyy-MM-dd_HH.mm.ss'
- $Extension = $FileURI | Select-Object -ExpandProperty Extension
- if (Test-Path $DestinationURI\$CreationTimeString$Extension) {
- if ((Get-Item $DestinationURI\$CreationTimeString$Extension).length -eq (Get-Item $SourceURI\$FileURI).length) {
- Write-Host "Already exists:`t`t$CreationTimeString$Extension = $FileURI"
- if (Test-Path variable:HashesFile) {
- $Hash = Get-FileHash $SourceURI\$FileURI -Algorithm SHA1 | Select-Object -ExpandProperty SHA1
- if ($Hash -in $HashesFile.SHA1) {Skip-File} else {Write-Host "Mismatch: $Hash $FileURI" -ForegroundColor Yellow -BackgroundColor Red}
- } else {Skip-File}
- } else {
- Write-Host "`aSize difference:`t$CreationTimeString$Extension ≠ $FileURI" -ForegroundColor Yellow -BackgroundColor Red
- Skip-File
- }
- } else {Copy-File}
- }
- <#
- # Verify hashes of the copied files and delete the verified source files.
- if (Test-Path variable:HashesFile) {
- foreach ($FileURI in Get-ChildItem $DestinationURI | Where-Object {$_.Name -NotMatch $HashesFileName}) {
- $Hash = Get-FileHash $DestinationURI\$FileURI -Algorithm SHA1 | Select-Object -ExpandProperty SHA1
- if ($Hash -in $HashesFile.SHA1) {
- Write-Host "Verified: $Hash $FileURI" -ForegroundColor DarkGreen
- } else {
- Write-Host "Mismatch: $Hash $FileURI" -ForegroundColor Red
- }
- }
- }
- #>
- # Dump table to CSV.
- $SHA1Table | Sort-Object DateTime | Export-Csv "$HashesFileURI" -Encoding UTF8 -Append
- <#
- Write-Host 'Table: SHA1' -ForegroundColor DarkGreen
- $SHA1Table | format-table -AutoSize
- Write-Host 'Table: HashesFile' -ForegroundColor DarkGreen
- $HashesFile | format-table -AutoSize
- Write-Host 'Table: SkippedHashes' -ForegroundColor DarkGreen
- $SkippedTable | format-table -AutoSize
- #>
- # End.
- $Date = Get-Date
- $EndTime = $Date.ToShortDateString() + ' ' + $Date.ToLongTimeString()
- Write-Host "At $EndTime the backup ended." -ForegroundColor Black -BackgroundColor White
- #<#
- Write-Host 'Press any key to end...'
- [void][System.Console]::ReadKey($true)
- #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement