Hi Everyone,
often , we do take photos and store it in our external drives with same name or different name.
ex: we have inserted the Camera memory card in our system and copy it to our specific drive or to other external drive. some time though images are same, file names might be different. we can do the image comparison using powershell hash. below is the script for this.
$Source= “C:\Users\Chaitanya\Pictures\Camera Roll”
$Destination = “C:\Users\Chaitanya\Pictures\Test\Testing”
$Destinationobj =get-childitem -Path $Destination -Recurse
$DestinationobjCount=$Destinationobj.Count
$DestObjArr=@();
$Destinationobj| ForEach-Object {
$DestinationobjCount–;
$DestHash=$null
$DestFileName = $_.Name
$DestFilePath = $_.FullName
$DestHash = Get-FileHash -Path $DestFilePath -Algorithm MD5
Write-Host “Creating Hash for Destination Image name $($DestFileName) and Current Image Count is : $($DestinationobjCount) ” -ForegroundColor cyan
if($DestHash)
{
$DestObjArr+=[pscustomobject]@{DestFileName=$DestFileName; DestFileHash=$($DestHash).Hash;}
}
}
$SourceObj =get-childitem -Path $Source -Recurse
$SourceObjCount=$SourceObj.Count
$SourceObj| ForEach-Object {
$SourceObjCount–;
$SrcFile = $_.FullName
$SRCFileName = $_.Name
$DestFilepath =$Destination+”\”+$SRCFileName
Write-Host “Creating Hash for Source Image name $($SRCFileName) and Current Image Count is : $($SourceObjCount)” -ForegroundColor cyan
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5
if ($SrcHash)
{
Write-Host “Checking the Destination Hash for Source File Name : $($SRCFileName) ” -ForegroundColor Cyan
if ($DestObjArr | Where-Object {$_.DestFileHash -eq $($SrcHash).Hash})
{
Write-Host “Hash Exists for Source Image Name $($SRCFileName)” -ForegroundColor DarkYellow
}
else
{
Write-Warning “Hash does Not Exists for Source Image Name :$($SRCFileName), Copying ..”
if (!(Test-Path $DestFilepath))
{
Write-Host “File Name not exists, Copying started for Source Image Name :$($SRCFileName)” -ForegroundColor White
Copy-Item $SrcFile -Destination $DestFilepath
}
else
{
Write-Host “File Name exists, create new name, Copying started for Source Image Name :$($SRCFileName)” -ForegroundColor Magenta
[string]$logdate= Get-Date -Format “yyyyMMddHHssmm”
$DestFilepath =$Destination+”\”+$SRCFileName.Split(“.”)[0]+ $($logdate)+”.”+$SRCFileName.Split(“.”)[1]
Copy-Item $SrcFile -Destination $DestFilepath
}
Write-Host “Copying complete for Source Image Name :$($SRCFileName)” -ForegroundColor Green
}
}
}
i hope with this your problem with the image saving in your external drives will be solved.
post your comments and let us your opinion on this.
regards,
Chay