I'm currently doing an application that migrates millions of images (.TIF) from one NAS to another, and I want to make a validation that allows me to check if the files were copied correctly.
The way I copy is with a function that does this:
Public Function CopyFiles(ByVal origin As String, ByVal copiedFile As String)
Try
'Check if file exists
If File.Exists(copiedFile) = False Then
My.Computer.FileSystem.CopyFile(origin, copiedFile)
Log("File copied succsessfully")
Else
Log("File already exists")
End If
Return True
Catch ex As Exception
Log("Error while copying file " + origin.ToString + " Error:" + ex.ToString)
End Try
Return False
I did have this file compare function:
Private Function FileCompare(ByVal file1 As String, ByVal file2 As String) As Boolean
'Compara byte a byte que los archivos sean iguales.
'ACTUALMENTE NO SE UTILIZA
Dim file1byte As Integer
Dim file2byte As Integer
Dim fs1 As FileStream
Dim fs2 As FileStream
Try
' Determine if the same file was referenced two times.
If (file1 = file2) Then
' Return 0 to indicate that the files are the same.
Return True
End If
' Open the two files.
fs1 = New FileStream(file1, FileMode.Open)
fs2 = New FileStream(file2, FileMode.Open)
' Check the file sizes. If they are not the same, the files
' are not equal.
If (fs1.Length <> fs2.Length) Then
' Close the file
fs1.Close()
fs2.Close()
' Return a non-zero value to indicate that the files are different.
Return False
End If
' Read and compare a byte from each file until either a
' non-matching set of bytes is found or until the end of
' file1 is reached.
Do
' Read one byte from each file.
file1byte = fs1.ReadByte()
file2byte = fs2.ReadByte()
Loop While ((file1byte = file2byte) And (file1byte <> -1))
' Close the files.
fs1.Close()
fs2.Close()
' Return the success of the comparison. "file1byte" is
' equal to "file2byte" at this point only if the files are
' the same.
If ((file1byte - file2byte) = 0) Then
'Log("******* Archivo Comparado correctamente= " + file1.ToString + " " + file2.ToString + " *******")
Return True
Else
Log("******* ERROR: al comparar archivos: " + file1.ToString + " " + file2.ToString + " *******")
Return False
End If
Catch ex As Exception
Log("******* ERROR, excepcion al comparar archivos: " + file1.ToString + " VS " + file2.ToString + " " + ex.ToString.ToUpper + " *******")
Return False
End Try
Return True
End Function
But it took too long when it started comparing byte by byte every single image, so I was thinking on some other ways to validate that the file has copied correctly.
So far what I have implemented is that I check that the copied file exists, but that doesn't assure me that it didn't copy with any issues.
So my ideas are:
Create a function that opens and closes the file just to check if it can open.
Create a function that compares the size of the original file and the copied one, but I don't know if there could be any case where the copied file has the same size but with errors.
Just leave a function that verifies that the copied file exists, since so far in all my tests, I haven't got any problem with my copied images.