Page 1 of 1

Compare The Files - small open-source tool

Posted: Tue Aug 10, 2010 1:08 pm
by jamirokwai
Hi board,

I am open-sourcing some of my snippets or smaller tools, I won't develop any further. Hope you find this usefull. It was tested on Mac OS X, and Windows XP.

Here is also a ZIP with the source-code, and some Icons I made: http://pb.quadworks.de/OSS/TinyCompare.zip

Code: Select all

;#########################
;#     Compare The Files #
;# (p) 2010 quadWorks.de #
;#########################

OpenWindow  (0,0,0,360,110,"Compare The Files", #PB_Window_WindowCentered | #PB_Window_ScreenCentered | #PB_Window_BorderLess)
ButtonGadget(0, 10, 10,340, 24, ".. please chose a file ..")
ButtonGadget(1, 10, 44,340, 24, ".. please chose a file ..")
ButtonGadget(2, 82, 78, 70, 24, "Compare")
TextGadget  (3,160, 84,300, 20, "Most likely different")
ButtonGadget(4, 10, 78, 20, 24, "?", #PB_Text_Center)
ButtonGadget(5, 36, 78, 40, 24, "Quit", #PB_Text_Center)
LoadFont    (0, "Arial", 11)
For i = 0 To 5 : SetGadgetFont(i,FontID(0)) : Next i
AddKeyboardShortcut(0, #PB_Shortcut_Command | #PB_Shortcut_Q, #PB_Event_CloseWindow)

Repeat
 Event = WaitWindowEvent(100)
 Select Event
  Case #PB_Event_Gadget
   Gadget = EventGadget()
  Select Gadget
   Case 0, 1
    temp$ = OpenFileRequester("Chose file", "", "", 0)
    If temp$ <> ""
     SetGadgetText(Gadget, "1. " + GetFilePart(temp$) + " - " + StrF(FileSize(temp$) / 1024, 3) + "kb - " + FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss", GetFileDate(Temp$, #PB_Date_Modified)))
     If Gadget = 0
      MD5a$ = MD5FileFingerprint(temp$)
      fileA$ = temp$
     Else
      MD5b$ = MD5FileFingerprint(temp$)
      fileB$ = temp$               
     EndIf
     GadgetToolTip(Gadget, Temp$)
    EndIf
  Case 2
   If MD5a$ = "" Or MD5b$ = ""
    MessageRequester("Information", "Please select two files to compare.")
   Else
    If MD5a$ = MD5b$ 
     SetGadgetText(3, "Most likely identical")
    Else
     SetGadgetText(3, "Most likely different")
    EndIf
   EndIf
   Case 4 : MessageRequester("Information", "TinyCompare compares the MD5-fingerprints of two files. If the fingerprints are identical, the chance is high, that the two files are identical.")
  EndSelect
 EndSelect
Until Event = #PB_Event_CloseWindow Or Event = #PB_Event_Menu Or Gadget = 5

Re: Compare The Files - small open-source tool

Posted: Tue Aug 10, 2010 5:12 pm
by flaith
A lot of gifts for the community :D
Thanks a lot Jamirokwai

Re: Compare The Files - small open-source tool

Posted: Tue Aug 10, 2010 5:49 pm
by jamirokwai
flaith wrote:A lot of gifts for the community :D
Thanks a lot Jamirokwai
Hi flaith,

you're welcome. FYI: I am cleaning up my projects-folder.

Going to put some more things into creative-commons, as I didn't work on them for at least 3 months now.
Hopefully the community can improve things, as I like to keep an eye on the result...

I will concentrate on 3 or 4 products, I am already selling via my webpage.

Re: Compare The Files - small open-source tool

Posted: Tue Aug 10, 2010 10:18 pm
by Vera
Hello jamirokwai,

thank you for sharing :D

I've run your code on Linux (Suse 11.1) and it worked right away 8)

and I really appreciate the concrete result of 'most likely' :lol:
and it already inspires me to several smaller improvements (to be posted when useful)

cheers ~ Vera

Re: Compare The Files - small open-source tool

Posted: Tue Aug 10, 2010 11:00 pm
by Rescator
Don't forget folks that a checksum will only guarantee with 100% certainty that two files are different,
but never if two files are the same.

I.e:
If the checksums are different then you can be 100% sure they are different.
But if the checksums are the same you can only be 99.99% (or around there) certain they are the same.

My advise is that if the two files to be compared are not directly available. (like when you have a list of checksums to compare the files against)
then using two or even three checksums might be best.

But if both files are directly available, then simply setting the file buffers to something like 64KB and then comparing blocks in blocks of memory
will be about the same speed as a checksum or crc but you get 100% confirmation. (byte comparisons done in block reads for a high speedup).

A very quick and early test that you should always do obviously is to check the filesize, if it's different then there is no need to do a checksum or byte compare at all.

PS!
the line: SetGadgetText(3, "Most likely different")
you can change that to: SetGadgetText(3, "Are different")
as due to the nature of checksums that statement is always guaranteed to be true, the other line however should remain "most likely" as it is now. ;)