Advertisement
DePhoegon

Mass Storage Moving

Jul 1st, 2025 (edited)
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.39 KB | Software | 0 0
  1. Param(
  2.     [Parameter(Mandatory = $true)]
  3.     [string]$Source,
  4.  
  5.     [Parameter(Mandatory = $true)]
  6.     [string]$Destination,
  7.  
  8.     [int]$BufferSizeMB = 4,
  9.     [string]$MoveFiles = "false"
  10. )
  11. $shouldMove = $false
  12. if ($MoveFiles.ToLower() -eq "true") {
  13.     $shouldMove = $true
  14. }
  15.  
  16. #HASH
  17. function Get-FileHashSHA256 {
  18.     param([string]$Path)
  19.     return (Get-FileHash -Algorithm SHA256 -Path $Path).Hash
  20. }
  21.  
  22. # Format: YYYY-MM-DD_HH-MM-SS for cross-platform friendliness
  23. $timestamp = (Get-Date -Format "yyyy-MM-dd_HH-mm-ss")
  24. $logFileName = "HashMismatchLog_$timestamp.txt"
  25. $logFilePath = Join-Path $Destination $logFileName
  26.  
  27. # Create an empty file to start with
  28. New-Item -ItemType File -Path $logFilePath -Force | Out-Null
  29.  
  30. # Prepare buffer size in bytes
  31. $bufferSize = $BufferSizeMB * 1MB
  32.  
  33. # Gather all files
  34. $files      = Get-ChildItem -Path $Source -Recurse -File
  35. $totalCount = $files.Count
  36. if ($totalCount -eq 0) {
  37.     Write-Host "No files found under $Source"
  38.     exit
  39. }
  40.  
  41. $currentIndex = 0
  42.  
  43. foreach ($file in $files) {
  44.     $currentIndex++
  45.  
  46.     $relative = $file.FullName.Substring($Source.Length).TrimStart('\')
  47.     $destFull = Join-Path $Destination $relative
  48.     $destDir  = Split-Path $destFull -Parent
  49.  
  50.     if (-not (Test-Path $destDir)) {
  51.         New-Item -ItemType Directory -Path $destDir | Out-Null
  52.     }
  53.  
  54.     $bytesCopied = 0
  55.     $fileSize    = $file.Length
  56.     $reader      = [System.IO.File]::OpenRead($file.FullName)
  57.     $writer      = [System.IO.File]::Create($destFull)
  58.  
  59.     try {
  60.         $buffer = New-Object byte[] ($BufferSizeMB * 1MB)
  61.  
  62.         while (($read = $reader.Read($buffer, 0, $buffer.Length)) -gt 0) {
  63.             $writer.Write($buffer, 0, $read)
  64.             $bytesCopied += $read
  65.  
  66.             $percentFile    = [math]::Round(($bytesCopied / $fileSize) * 100, 1)
  67.             $percentOverall = [math]::Round(($currentIndex / $totalCount) * 100, 1)
  68.  
  69.             Write-Progress `
  70.                 -Activity "Copying file $currentIndex of $totalCount" `
  71.                 -Status "$percentFile%% of $($file.Name)" `
  72.                 -PercentComplete $percentOverall
  73.         }
  74.     }
  75.     finally {
  76.         $reader.Close()
  77.         $writer.Close()
  78.     }
  79.  
  80.     # Verify hash match
  81.     $srcHash = Get-FileHashSHA256 -Path $file.FullName
  82.     $dstHash = Get-FileHashSHA256 -Path $destFull
  83.  
  84.     if ($srcHash -eq $dstHash) {
  85.         Write-Host "✅ Verified: $relative"
  86.  
  87.         if ($shouldMove) {
  88.             try {
  89.                 Remove-Item $file.FullName -Force
  90.                 Write-Host "🗑️ Moved (source deleted): $relative"
  91.             } catch {
  92.                 Write-Warning "⚠️ Failed to delete source file: $relative"
  93.             }
  94.         } else {
  95.             Write-Host "Copied: $relative"
  96.         }
  97.     } else {
  98.         Write-Warning "❌ Hash mismatch for $relative — file NOT deleted!"
  99.         $logEntry = @"
  100.        SOURCE: $($file.FullName)
  101.        DEST:   $destFull
  102.  
  103. "@
  104.         Add-Content -Path $logFilePath -Value $logEntry
  105.     }
  106. }
  107.  
  108. # Finalize progress
  109. Write-Progress -Activity "Copy Complete" -Completed
  110. Write-Host "All $totalCount files copied from `"$Source`" to `"$Destination`"."
  111. if ((Get-Content $logFilePath).Length -eq 0) {
  112.     Remove-Item $logFilePath -Force
  113.     Write-Host "✅ No hash mismatches detected — log file auto-deleted."
  114. } else {
  115.     Write-Host "⚠ Hash mismatches logged in: $logFilePath"
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement