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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verify file copy to floppy

Status
Not open for further replies.

mainbyte

Programmer
Feb 16, 2001
6
US
Does anyone have a suggestion on how to copy a file from the harddrive and copy it to the floppy and verify. (I am now using FileCopy), and this works fine but I would like to verify that the copy was good, similar to copy /v . I would assume that there is a way to do this without using shell and a dos command.

Thank you

Ron
 
This is actually pretty simple. The trick is to use VB Binary I/O and dynamic byte arrays.

You didn't say how large these files are. This sample works in chunks and copies/verifies any size file in chunks as specified by a global constant. The larger the chunk the faster, but there is a memory penalty in making them TOO large:

Code:
Option Explicit
Const clngChunk = 50000
Dim bytOrig() As Byte
Dim bytCopy() As Byte
Dim lngRemaining As Long
Dim lngRemaining2 As Long

Private Sub CopyFile()
  Label1.Enabled = True

  ReDim bytOrig(clngChunk - 1)
  Open "mydata.txt" For Binary As #1
  Open "copydata.txt" For Binary As #2
  lngRemaining = LOF(1)
  
  While lngRemaining >= clngChunk
    Get #1, , bytOrig
    Put #2, , bytOrig
    lngRemaining = lngRemaining - clngChunk
  Wend

  If lngRemaining > 0 Then
    ReDim bytOrig(lngRemaining - 1)
    Get #1, , bytOrig
    Put #2, , bytOrig
  End If
  
  Close #2
  Close #1
  Check1.Value = vbChecked
  Label3.ForeColor = vbGreen
  Label3.Caption = "Copy Successful"
End Sub

Private Sub CompareFailed()
    Label3.ForeColor = vbRed
    Label3.Caption = "Compare failed!"
    Close #2
    Close #1
End Sub

Private Sub CompareFile()
  Label2.Enabled = True

  ReDim bytOrig(clngChunk - 1)
  ReDim bytCopy(clngChunk - 1)
  Open "mydata.txt" For Binary As #1
  Open "copydata.txt" For Binary As #2
  lngRemaining = LOF(1)
  lngRemaining2 = LOF(2)
  
  If lngRemaining <> lngRemaining2 Then
    CompareFailed
    Exit Sub
  End If
  
  While lngRemaining >= clngChunk
    Get #1, , bytOrig
    Get #2, , bytCopy
    If StrComp(bytOrig, bytCopy, vbBinaryCompare) <> 0 Then
      CompareFailed
      Exit Sub
    End If
    lngRemaining = lngRemaining - clngChunk
  Wend

  If lngRemaining > 0 Then
    ReDim bytOrig(lngRemaining - 1)
    ReDim bytCopy(lngRemaining - 1)
    Get #1, , bytOrig
    Get #2, , bytCopy
    If StrComp(bytOrig, bytCopy, vbBinaryCompare) <> 0 Then
      CompareFailed
      Exit Sub
    End If
  End If

  Close #2
  Close #1
  Check2.Value = vbChecked
  Label3.ForeColor = vbGreen
  Label3.Caption = &quot;Compare Successful&quot;
End Sub

Private Sub Form_Load()
  CopyFile
  CompareFile
End Sub
It is used with a form that has 3 labels and 2 checkboxes on it to show progress/results:
Code:
{Check1} Copying file
{Check2} Comparing files
         {Label3}
The first two labels are disabled initially so they appear &quot;grayed out.&quot;
 
If you are using NT or 2000 you might try CopyFileEx. I believe the function will inform you when there are any errors copying the file. (I guess the arguable point is, &quot;Can Windows make an invalid copy of a file?&quot;)
[tt]
Private Sub Form_Load()
Dim Ret As Long
Me.AutoRedraw = True
Me.Print &quot;Click to abort file copy&quot;
Me.Show
Ret = CopyFileEx(&quot;c:\MyBigFile.qaz&quot;, _
&quot;c:\MyCopiedFile.qaz&quot;, _
AddressOf CopyProgressRoutine, _
ByVal 0&, _
bCancel, _
COPY_FILE_RESTARTABLE)
Me.Print &quot;Filecopy completed &quot; _
+ IIf(Ret = 0, &quot;(ERROR/ABORTED)&quot;, _
&quot;successfully&quot;)
End Sub

Private Sub Form_Click()
bCancel = 1
End Sub
[/tt]

In a module=====================================================
[tt]
Public Const PROGRESS_CANCEL = 1
Public Const PROGRESS_CONTINUE = 0
Public Const PROGRESS_QUIET = 3
Public Const PROGRESS_STOP = 2
Public Const COPY_FILE_FAIL_IF_EXISTS = &H1
Public Const COPY_FILE_RESTARTABLE = &H2
Public Declare Function CopyFileEx _
Lib &quot;kernel32.dll&quot; _
Alias &quot;CopyFileExA&quot; _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As Long, _
lpData As Any, _
ByRef pbCancel As Long, _
ByVal dwCopyFlags As Long) As Long
Public bCancel As Long
Public Function CopyProgressRoutine _
(ByVal TotalFileSize As Currency, _
ByVal TotalBytesTransferred As Currency, _
ByVal StreamSize As Currency, _
ByVal StreamBytesTransferred As Currency, _
ByVal dwStreamNumber As Long, _
ByVal dwCallbackReason As Long, _
ByVal hSourceFile As Long, _
ByVal hDestinationFile As Long, _
ByVal lpData As Long) As Long
Form1.Caption = CStr(Int((TotalBytesTransferred * 10000) _
/ (TotalFileSize * 10000) * 100)) + &quot;% complete...&quot;
DoEvents
CopyProgressRoutine = PROGRESS_CONTINUE
End Function
[/tt]

....Or you could do it the old-fashioned way, as Dilettante suggested. (It actually leads the programmer to believe he has a bit more control.) Here's an abbreviated version that makes a simple string comparison. If anybody tries this and finds that it fails to perform the task, please let me know.
[tt]
On Error GoTo ErrorTrap
MyFile$ = &quot;MyXfile.qaz&quot;
Open MyFile$ For Binary As #1
G1$ = String$(LOF(1), 0)
Get #1, 1, G1$
Close #1
Open &quot;A:\&quot; & MyFile$ For Binary As #1
Put #1, 1, G1$
Close #1
Open &quot;A:\&quot; & MyFile$ For Binary As #1
G2$ = String$(LOF(1), 0)
Get #1, 1, G2$
Close #1
If G2$ = G1$ Then
MsgBox &quot;File copy verified.&quot;
Exit Sub
End If
ErrorTrap:
Er = Err
Close
If Er > 0 Then AddErr$ = &quot;: &quot; & Error(Err)
MsgBox &quot;Error copying file&quot; & AddErr$
[/tt]

VCA.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top