Hi All,

I need to compare one file in each file in the folders, some sort of duplicate check through md5 hashcode that I've found in my google search.

Here's the code of functions:

Public Function CompareFiles(ByVal FirstFile As String, _
        ByVal SecondFile As String) As Boolean
        Return ReadFile(FirstFile) = ReadFile(SecondFile)
    End Function
    Private Function ReadFile(ByVal Path As String) As String
        Dim ReadFileStream As FileStream
        Dim FileEncoding As New System.Text.ASCIIEncoding()
        Dim FileReader As StreamReader
        Dim HashData As New MD5CryptoServiceProvider()
        ReadFileStream = New FileStream(Path, FileMode.Open)
        FileReader = New StreamReader(ReadFileStream)
        Dim FileBytes = FileEncoding.GetBytes(FileReader.ReadToEnd)
        Dim FetchedContent = FileEncoding.GetString(HashData.ComputeHash(FileBytes))
        FileReader.Close()
        ReadFileStream.Close()
        Return FetchedContent

And this is how to use:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (CompareFiles("path of first file", "path of second file")) Then
            MsgBox("duplicate")
        Else
            MsgBox("diff")
        End If
    End Sub

I have a hot folder wherein my client sends a zip file and a back up folder that contains previous files from my client.

Now, what should be my modification of the above usage for me to compare the incoming file (for example test.zip) from hot folder to each file in the backup folder?

I write this code but no luck:

Dim dir As New DirectoryInfo(d:\backup)
                        Dim zfiles As FileInfo() = dir.GetFiles("*.zip")
                        Dim zfile As FileInfo
                        For Each zfile In zfiles
                            If (CompareFiles("c:\hotfolder\test.zip", "d:\backup" & zfile.ToString)) Then
                                MsgBox("duplicate")
                            Else
                                MsgBox("different")                            
End If
                        Next

Here is a working example

Sub Main()

		If not CompareFiles("C:\Users\GeekByChoiCe\Desktop\dummy.txt", "C:\Users\GeekByChoiCe\Desktop\Backup") Then
			Console.WriteLine("file does not exist")
		Else
			Console.WriteLine("File already exist.")
		End If

		Console.Read()

	End Sub

	Private Function CompareFiles(newFile As String, oldDirectory As String) As Boolean
		Dim newFileMD5 As String = HashMD5(newFile)
		Dim oldFiles() As String = Directory.GetFiles(oldDirectory, "*", SearchOption.AllDirectories)
		For Each oldFile As String In oldFiles
			If HashMD5(oldFile) = newFileMD5 Then
				Return True	'file already exist
			End If
		Next
		Return False
	End Function


	Private Function HashMD5(ByVal imgFile As String) As String

		Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
		Dim f As FileStream = New FileStream(imgFile, FileMode.Open, FileAccess.Read)
		md5.ComputeHash(f)
		Dim hash As Byte() = md5.Hash
		Dim buff As New StringBuilder
		For Each hashByte As Byte In hash
			buff.Append(String.Format("{0:X2}", hashByte))
		Next
		f.Close()
		Return buff.ToString.ToLower

	End Function

Hi GeekByChoiCe, thanks for the reply. I'll try it now.

Follow up question, what if there's no files in backup folder?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.