Created
July 29, 2024 02:10
-
-
Save Luen/504bdbb3bc4fa896861011bacb4f077e to your computer and use it in GitHub Desktop.
Compare files in Windows Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$originalFile = "C:\Users\luenw\Downloads\original.img" | |
$downloadedFile = "C:\Users\luenw\Downloads\downloaded.img" | |
$originalHash = Get-FileHash -Path $originalFile -Algorithm MD5 | |
$downloadedHash = Get-FileHash -Path $downloadedFile -Algorithm MD5 | |
if ($originalHash.Hash -eq $downloadedHash.Hash) { | |
Write-Output "Checksum: The files are identical." | |
} else { | |
Write-Output "Checksum: The files are different." | |
} | |
$chunkSize = 1024 * 1024 # 1MB chunks | |
# Open files for reading | |
$originalStream = [System.IO.File]::OpenRead($originalFile) | |
$downloadedStream = [System.IO.File]::OpenRead($downloadedFile) | |
$bufferOriginal = New-Object byte[] $chunkSize | |
$bufferDownloaded = New-Object byte[] $chunkSize | |
$identical = $true | |
try { | |
while ($true) { | |
$bytesReadOriginal = $originalStream.Read($bufferOriginal, 0, $chunkSize) | |
$bytesReadDownloaded = $downloadedStream.Read($bufferDownloaded, 0, $chunkSize) | |
if ($bytesReadOriginal -ne $bytesReadDownloaded) { | |
$identical = $false | |
break | |
} | |
if ($bytesReadOriginal -eq 0) { | |
break | |
} | |
for ($i = 0; $i -lt $bytesReadOriginal; $i++) { | |
if ($bufferOriginal[$i] -ne $bufferDownloaded[$i]) { | |
$identical = $false | |
break | |
} | |
} | |
if (-not $identical) { | |
break | |
} | |
} | |
} finally { | |
$originalStream.Close() | |
$downloadedStream.Close() | |
} | |
if ($identical) { | |
Write-Output "Chucks: The files are identical." | |
} else { | |
Write-Output "Chucks: The files are different." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment