How to check if two files are identical

Post

Posted
Rating:
#1 (In Topic #1174)
Trainee
 Hi all

Is there a way to check if two files are identical?

I notice that there is a gb.Hash class where you can get a hash value of some specified text, but this doesn't give the hash value of a specified file.

I want to be able to say;
a=hash(file1)
b=hash(file2)

does a=b?

Thanks for looking
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 The quick answer is to shell out to the standard "diff" utility, using the "-s" option. This save you having to load the files. (More later, I'm a bit tired)
b

Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
I agree with thatbruce. The diff command will do what you need. Have a look at the code below.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim sFile1 As String = User.Home &/ "Diff1"
  4.   Dim sFile2 As String = User.Home &/ "Diff2"
  5.   Dim sResult As String
  6.  
  7.   Shell "diff " & sFile1 & " " & sFile2 To sResult
  8.  
  9.   If Trim(sResult) = "" Then  
  10.     Print "Files are the same"
  11.   Else
  12.     Print "Files are different"
  13.   End If
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
vuott is in the usergroup ‘Regular’
The native Comp() function may also be fine, , loading the two files with the "File.Save()" Method.
   /lang/comp - Gambas Documentation


If you want to check the equality of two files using the external functions of the libgio API:
    https://www.gambas-it.org/wiki/index.php/Controllare_l%27uguaglianza_di_due_file_mediante_le_funzioni_esterne_del_API_di_libgio


Of course, you could also write an "ad hoc " function:

Code (gambas)

  1. Private Function Compare(pathfile1 As String, pathfile2 As String) As Boolean
  2.  
  3.   Dim s1, s2 As String
  4.   Dim bo As Boolean = True
  5.  
  6.   s1 = File.Load(pathfile1)
  7.   s2 = File.Load(pathfile2)
  8.  
  9.   If s1.Len <> s2.Len Then
  10.     bo = False
  11.   Else
  12.     For i = 0 To s1.Len - 1
  13.       If s1[i] <> s2[i] Then bo = False
  14.     Next
  15.  
  16.   Return bo
  17.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
Both options work, I'll toss a coin to decide which one to use  :D

Thanks for your time.

Pusherman
Online now: No Back to the top
1 guest and 0 members have just viewed this.