Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verify FileCopy 3

Status
Not open for further replies.

waytech2003

Programmer
Jul 14, 2003
316
US
I need to add the ability to backup an Access database, and many pictures in a sub directory, to a Zip Disk. I have used several differant methods before (filecopy) (FSO) and was wondering if any of these have the ability to Verify that the info was written correcty. Maybe this is built into the copy function's and I just do not know about it.[blush]

Is there a way to compare a file to the original? I do not want to just check date/time and filesize, I need to know that it is a GOOD copy.
 
Hi waytech,

One thing you could do, although it would slow your application down a lot, would be to load both files into a ByteArray and compare the value of each element with eachother. Any discrepancies would indicate something has gone wrong saving to your Zip drive.
 
VBrit, thanks for the quit reply.

I might work on something like that, but I hope someone come up with a ready made procedure that will do it for me. I thought there might be some sort of API call.

I will wait to see if there are other reply's.
 
Doesn't the old DOS comp command do exactly what you're looking for? If so, you could just shell out to it or some updated version.

Code:
C:\Documents and Settings\rrodes\My Documents>copy ucc.doc temp
        1 file(s) copied.

C:\Documents and Settings\rrodes\My Documents>comp temp ucc.doc
Comparing temp and ucc.doc...
Files compare OK

Compare more files (Y/N) ? n

C:\Documents and Settings\rrodes\My Documents>
 
BobRodes, Yes the DOS COMP does do a compare, I just do not know how I would get the results back in my VB program to see if they failed or not.

strongm, I think Xcopy /V is what I was looking for.

Thanks to all that replied
 
Well it appears that the Verify switch does not do what I need. The info below was copied from Microsoft's website. I will have to keep looking for a different approach.


SUMMARY
Some versions of the MS-DOS "User's Guide" and online Help indicate that the /V (verify) switch for the COPY and XCOPY commands actually compares the source and destination files to determine whether they are identical. This is not correct. The /V switch verifies that the destination file, once written, can be read. No comparison of the files occurs.
 
waytech2003,

I am sure that the only way you can verify the contents of the file is to do just that. Load the source files contents, load the target files contents, and make sure they're the same!

You could stream the files, and compare after each 1024k(for example) if you wanted a progress counter and to not hog the processor for the thread. You'd need to use the FileSystemObject for this.
 
As a little task to keep me busy (bored at work), do you want me to write a version of this for you?
 
VBrit

Thanks for the offer, but I think I have found some code from MS that will do the job. If it turns out that it does not work, I may come back to take you up on your offer.

p.s.
Wished I had the time to get bored...
 
waytech2003,

Could you please post a link to the code you found so that others may benefit from your experience?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I found another solution for you. It is similar to Bob's method but uses the FC command instead of COMP. The commands are similar, but FC does not prompt you to compare more files.

With the help of some code from Hypetia in another thread, I threw this together for you. The ShellWait function is critical here because you will use the FC command to compare the files. The output of the FC command is redirected to a file. The next line of code opens the file to examine the results. You need ShellWait so that you don't open the file until it is created.

Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Const SYNCHRONIZE = &H100000
Const PROCESS_QUERY_INFORMATION = &H400&
Const INFINITE = -1&
'--- Shells the passed command line and waits for the process to finish
'--- Returns the exit code of the shelled process
Public Function ShellWait(PathName As String, Optional ShowCommand As VbAppWinStyle = vbNormalFocus) As Long
    Dim ProcessId As Long, hProcess As Long

    'run the process and get its process id
    ProcessId = Shell(PathName, ShowCommand)

    'open the process handle
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or SYNCHRONIZE, 0, ProcessId)

    'wait until the process terminates
    WaitForSingleObject hProcess, INFINITE

    'get the exit code
    GetExitCodeProcess hProcess, ShellWait

    'close the proces handle
    CloseHandle hProcess
End Function

Private Function CompareFiles(ByVal File1 As String, ByVal File2 As String) As Boolean
        
    ' This function returns TRUE of the files are the same
    Dim OutputFile As String
    Dim ShellCommand As String
    Dim FSO As Scripting.FileSystemObject
    Dim strTemp As String
    
    OutputFile = App.Path & "\Compare.txt"
    
    CompareFiles = False
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.GetFile(File1).Size = FSO.GetFile(File2).Size Then
        If FSO.FileExists(OutputFile) Then
            Call FSO.DeleteFile(OutputFile)
        End If
        
        ShellCommand = "CMD /c FC """ & File1 & """ """ & File2 & """ > """ & OutputFile & """"
        Call ShellWait(ShellCommand, vbNormalFocus)
        strTemp = FSO.OpenTextFile(OutputFile, ForReading).ReadAll
        If InStr(strTemp, "FC: no differences encountered") > 0 Then
            CompareFiles = True
        End If
    End If
    
End Function

Private Sub Command1_Click()
    
    If CompareFiles("C:\Path\File.ext", "C:\OtherPath\File.ext") Then
        MsgBox "Files are the same"
    Else
        MsgBox "Files are different"
    End If
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Here's what I've come up with (just drop the code ina module):
Code:
[blue]Option Explicit

Private Type MSIFILEHASHINFO
    dwFileHashInfoSize As Long
    strHashBuffer  As String * 16
End Type

Private Declare Function msiGetFileHash Lib "msi.dll" Alias "MsiGetFileHashA" (ByVal szFilePath As String, ByVal dwOptions As Long, pHash As Any) As Long 'MSIFILEHASHINFO) As Long 'dunno ...

Public Function FileMatch(strfile1 As String, strfile2 As String) As Boolean
    Dim pHash As MSIFILEHASHINFO
    Dim strFirstHash As String * 16
    
    pHash.dwFileHashInfoSize = Len(pHash)
    msiGetFileHash strfile1, 0&, pHash
    strFirstHash = pHash.strHashBuffer
    msiGetFileHash strfile2, 0&, pHash
    FileMatch = (strFirstHash = pHash.strHashBuffer)
End Function[/blue]
 
strongm, I did test your code as it was a quick and simple cut/paste operation. It did work.

There is one thing I do not know for sure, that is, does this just check Date/Size etc, or does it truly check each file for exactness. I do not know what the "GetFileHash" is about.
 
Well then I got what I needed...

I will give a star to all who helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top