Compare binaryfiles with AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Compare binaryfiles with AHK

07 Dec 2017, 08:51

It's easy to retrieve the size, datetime stamp of a file or folder, and then compare the result with another file.
But I don't found an instruction like this ..: FileCompare ...

Is it difficult to compare the binary content of two files with AHK?
Will the solution be slow?
Guest

Re: Compare binaryfiles with AHK

07 Dec 2017, 08:57

Calculate a hash of a file crc32, md5 demo with Gui https://github.com/jNizM/HashCalc although I believe https://github.com/jNizM/AHK_CNG is the 'latest' code by jNizM (check the hash functions)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Compare binaryfiles with AHK

07 Dec 2017, 14:23

This is the function that I use currently:

Code: Select all

JEE_FilesMatchContents(vPath1, vPath2)
{
	FileGetSize, vSize1, % vPath1
	FileGetSize, vSize2, % vPath2
	if !(vSize1 = vSize2)
		return 0
	if (vSize1 = 0) && (vSize2 = 0)
		return 1
	;FileRead, vData1, % "*c " vPath1
	;FileRead, vData2, % "*c " vPath2
	oFile := FileOpen(vPath1, "r")
	oFile.Pos := 0
	oFile.RawRead(vData1, oFile.Length)
	oFile.Close()
	oFile := FileOpen(vPath2, "r")
	oFile.Pos := 0
	oFile.RawRead(vData2, oFile.Length)
	oFile.Close()
	return !DllCall("msvcrt\memcmp", Ptr,&vData1, Ptr,&vData2, UPtr,vSize1, "Cdecl Int")
}
Some related info:
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 5 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 80#p169780

The problem with the function above is that it loads the file into memory before comparing, I don't know if there's a better approach that operates on files directly.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
KeypressGuy
Posts: 16
Joined: 17 Jul 2017, 16:55

Re: Compare binaryfiles with AHK

08 Dec 2017, 10:56

Here's a method from Christian Ghisler that takes 3 approaches.
1. Check file sizes
If identical,
2. Compare small pieces of the files
If identical,
3. Calculate MD5

Because this method is performing small spot checks rather than reading the entire file, it often detects differences very quickly.

Wiki http://www.ghisler.ch/wiki/index.php/Au ... le_compare
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Compare binaryfiles with AHK

13 Dec 2017, 06:06

For comparing two files, jeeswg's function is the best suggestion here imo. There is no point in calculating hashes in this case. In case of very big files I'd avoid reading the entire file unconditionally. You could loop chunks of 50-500 mb perhaps. One could even start at a small size and then increase. If you want to compare many files against each other, hash is good.

Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 155 guests