我有一个脚本,该脚本通过其文件哈希进行比较两个图像:
if((Get-FileHash C:UsersUserNameDesktopOrImg).hash -ne (Get-FileHash C:UsersUserNameDesktopRefImg).hash)
{"Files are different"}
else {"Files are the same"}
但是我如何确定哈希属性有两个图像是不同的?出于测试目的,我创建了两个不同的图像(大小,内容),但是当我比较它们时 - 哈希是相同的。也许我做错了什么?
这将返回使用不同文件哈希的所有文件。注意:这将不会检查文件是否存在于目录两个中,也不会检查目录两个目录是否包含目录一号不包含的文件。
$leftDir = "D:tmp1"
$rightDir = "D:tmp2"
$differentFiles = Get-ChildItem $leftDir | Where-Object {
($_ | Get-FileHash).Hash -ne (Get-FileHash (Join-Path $rightDir $_.Name)).Hash
}
if (-not $differentFiles)
{
'All files are the same'
}
else
{
$differentFiles | Foreach-Object {Write-Output "File $($_.Name) is different"}
}
我用MD5总和为我的测试脚本完成了此Windows PowerShell。这对我来说很好。
$FilePathSource = "C:SusanthaTest.txt"
$FilePathDestination = "C:SusanthaTest.txt"
try
{
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hashedFileSource = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePathSource)))
$hashedFileDestination = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePathDestination)))
[bool]$Result = $hashedFileSource.CompareTo($hashedFileDestination)
if ($Result) {
Write-Host "Not the Same Files :("
}
else{
Write-Host "Same Files :)"
}
}
catch
{
Write-Host $_.Exception.Message
Write-Host $_.Exception.ItemName
Break
}
尝试此
$x = (Get-FileHash 'C:temp1.bmp').hash
$z = (Get-FileHash 'C:temp2.bmp').hash
[bool]$w = $z.CompareTo($x)
if ($w) {
Write-Host "Not the same"
}
我问了关于我写的脚本的同样的问题,该脚本完成了一半的工作。这是完成的文章。
参考此处比较目录和子目录,并基于MD5 Hash
替换文件感谢@lotpings的帮助
$Source= "D:Folder1"
$Destination = "D:folder2"
get-childitem $Source -Recurse | foreach {
#Calculate hash using Algorithm MD5 reference http://www.tomsitpro.com/articles/powershell-file-hash-check,2-880.html
$SrcFile = $_.FullName
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5
$DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination
Write-Host "Copying $SrcFile to $DestFile" -ForegroundColor Yellow
if (Test-Path $DestFile) {
#Check the hash sum of the file copied
$DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
#compare them up
if ($SrcHash.hash -ne $DestHash.hash) {
Write-Warning "$SrcFile and $DestFile Files don't match!"
Copy-Item $SrcFile -Destination $DestFile -Force
} else {
Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor Green
}
} else {
Copy-Item $SrcFile -Destination $DestFile -Force
}
}
我进行了略微更改。不知道为什么您有2 x其他语句。副本下降了两次。下面的更改将显示为红色的应对。
$Source = "folder1"
$Destination = "folder2"
get-childitem $Source -Recurse | foreach {
#Calculate hash using Algorithm MD5 reference http://www.tomsitpro.com/articles/powershell-file-hash-check,2-880.html
$SrcFile = $_.FullName
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5
$DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination
Write-Host
Write-Host "Comparing $SrcFile to $DestFile" -ForegroundColor Yellow
if (Test-Path $DestFile) {
#Check the hash sum of the file copied
$DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
#compare them up
if ($SrcHash.hash -ne $DestHash.hash) {
Write-Warning "$SrcFile and $DestFile Files don't match!"
Write-Host "Copying $SrcFile to $DestFile" -ForegroundColor Red
Copy-Item $SrcFile -Destination $DestFile -Force
} else {
Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor Green
}
}
}