param
(
[string]$root = $null,
[switch]$verbose
);
#----------------------------------------------------------------
function filedup()
{
$dupfiles = @{}
$hashAlgorithm = new-object -TypeName "System.Security.Cryptography.MD5CryptoServiceProvider";
$files = @(Get-ChildItem -Path $root -recurse -ErrorAction SilentlyContinue);
foreach ($file in $files)
{
#"MD5($($file.PsIsContainer))"
if(!$file.PsIsContainer)
{
$stream = $file.OpenRead();
$hashByteArray = $hashAlgorithm.ComputeHash($stream);
$stream.Close();
$md5StringBuilder = New-Object System.Text.StringBuilder
$hashByteArray | % {[void] $md5StringBuilder.Append($_.ToString("x2"))}
$md5h = $md5StringBuilder.ToString()
if($verbose)
{
"Processed $($file.Fullname) with hash $md5h"
}
if(!$dupfiles.ContainsKey($md5h))
{
$dupfiles[$md5h] = @()
}
$dupfiles[$md5h] = $dupfiles[$md5h] + ($file.Fullname);
}
# Ensure stream is closed if exceptions occurred.
trap
{
if ($stream -ne $null) { $stream.Close(); }
break;
}
}
""
"Showing duplicate files:"
foreach ($item in $dupfiles.GetEnumerator())
{
if($item.value.length -gt 1)
{
"------------------------"
foreach($f in $item.value)
{
" $f"
}
}
}
"------------------------"
}
filedup -root $root -verbose $verbose;
C:\path-to-filedup> filedup [path]