2017-03-06 27 views
1

我有一個腳本,通過它們的文件哈希比較兩個圖像:比較兩個BMP文件以Get-FileHash

if((Get-FileHash C:\Users\UserName\Desktop\OrImg).hash -ne (Get-FileHash C:\Users\UserName\Desktop\RefImg).hash) 
{"Files are different"} 
else {"Files are the same"} 

但我怎麼能確定兩個圖像是通過hash屬性有什麼不同?爲了測試目的,我創建了兩個不同的圖像(大小,內容),但是當我比較它們時 - 哈希值是相同的。也許我做錯了什麼?

+0

我沒有看到您的代碼中的任何錯誤,這應該工作! –

+1

「我創建了兩個不同的圖像(大小,內容),但是當我比較它們時--HASH是一樣的。」 - 不,你沒有。這實際上是一個1在340282366920938463463374607431768211456的機會 –

+0

@MartinBrandl我的錯誤。我已經比較了文件夾中的兩個哈希文件夾不是文件。 '$ varf1 = Get-ChildItem「Path_To_The_Folder」-Recurse | foreach {Get-FileHash -Path $ _ FullName}' - 文件夾內的文件HASH如何進行比較。 –

回答

2

這將返回不同的文件哈希值的所有文件。注意:這不會檢查文件是否存在於目錄2中,也不會檢查目錄2是否包含目錄不存在的目錄。

$leftDir = "D:\tmp\1" 
$rightDir = "D:\tmp\2" 

$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"} 
} 
+0

太好了,馬丁!這幾乎是我需要的。但是,如何添加腳本的結果並將其解析爲Write-Output「文件相同」(如果所有文件相同)或Write-Output「文件$ nameofthefile(s)是不同的」? –

+0

看到我編輯的答案。 –

+0

太棒了,馬丁!但讓我們假設兩個文件夾中的所有文件都是相同的,我會寫 - 如果(上面的函數) - 兩個文件夾中的所有文件都是相同的 - 寫輸出「所有文件都是相同的」。 如果(你的函數在上面) - 這兩個文件夾中的一些文件是不同的 - 寫輸出「File $($ _。Name)是不同的」? –

0

試試這個

$x = (Get-FileHash 'C:\temp\1.bmp').hash 
$z = (Get-FileHash 'C:\temp\2.bmp').hash 
[bool]$w = $z.CompareTo($x) 
if ($w) { 
Write-Host "Not the same" 
} 
+0

感謝您的建議,但它是關於一個兩個文件(如果每個文件夾中有一個文件)。我有2個文件夾中的3個文件。兩個文件夾中的每個罰款名稱都相同 - 例如:目錄1中的file1目錄2中的文件1,目錄2中的file2目錄2中的文件2等等我上面已經提到過,如何解決壓縮多個文件的問題在兩個文件夾內。 –

0

我問了同樣的問題,我寫了一個腳本,做了一半的工作。這是完成的文章。這裏

參考Compare directories and sub directories and replace files based on 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 
} 
} 
+0

感謝您的建議。 –

0

我做了細微的變化。不知道爲什麼你有2個x else語句。複製下來兩次。 以下更改將顯示紅色應對。

$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 
    } 

} 
}