开发者

Use PowerShell to quickly test whether the content of two files is identical

开发者 https://www.devze.com 2023-04-13 05:12 出处:网络
As part of a larger PowerShell script, I want to test whether the con开发者_如何学运维tents of two binary files are identical.

As part of a larger PowerShell script, I want to test whether the con开发者_如何学运维tents of two binary files are identical.

I think the following is logically correct:

if (@(Compare-Object 
    $(Get-Content f1.txt -encoding byte) 
    $(Get-Content f2.txt -encoding byte) 
    -sync 0).length -eq 0) {
    "same" 
} else {
    "different"
}

However, the above runs very slowly as it's really using Compare-Object for something that is begging for a much simpler implementation.

I'm looking for something that gives the same logical result, but uses some faster low level file comparison.

I do not need or want any description of the differences, or any text output, just a logical test that gives me a boolean result.


If the files are large, causing compare-object to take much time, you can generate SHA1 hash and compare it.

Or you can read the files byte-by-byte in a loop, breaking at the first non-equal bytes.


if ($(Get-FileHash $fileA).Hash -ne $(Get-FileHash $fileB).Hash) {
  Write-Output "Files $fileA and $fileB aren't equal"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号