VB6 FileCopy destination lock failure leaves restored data unverified
0 reputation · 24 Jun 2020, 09:01 UTC
0 reputation · 24 Jun 2020, 09:01 UTC
When restoring application data files in a Visual Basic 6.0 environment, the intrinsic FileCopy statement fails if the destination is open, locked, or read-only, and it does not preserve original timestamps, attributes, or access-control metadata. The FileSystemObject.CopyFile method offers different overwrite rules and error surfacing, but neither approach declares a single preferred restore path, and VB6 provides no intrinsic file-hash or byte-comparison API to confirm that recovered bytes match the source.
Because a successful copy does not prove semantic validity or metadata fidelity, the choice between the two copy mechanisms remains an unresolved component-level decision. Behavior may also differ under VBA hosts or VB.NET, which use distinct file and security models.
Which copy mechanism should a VB6 backup restore use to balance error visibility and metadata preservation? How can recovered data be verified without an intrinsic hash or comparison API? Does the preferred restore path change when the target runs under a VBA host rather than the VB6 IDE?
35775 reputation · 24 Jun 2020, 18:22 UTC
For a backup restore operation in VB6, the FileSystemObject (FSO) is the preferred mechanism over the intrinsic FileCopy statement. While neither method natively preserves full NTFS metadata or original timestamps, FSO provides a critical advantage: the Overwrite parameter. This allows the developer to explicitly define behavior when a destination file exists, and the COM-based error surfacing is generally more granular than the runtime errors triggered by FileCopy.
To balance error visibility and reliability, use the following approach:
Microsoft Scripting Runtime.FSO.CopyFile source, destination, True.Try...Catch equivalent (On Error GoTo) to specifically trap permission errors or destination locks.Since VB6 lacks an intrinsic hashing API, data integrity must be verified through a manual binary comparison. This ensures that the bytes written to the disk match the source exactly, bypassing the assumption that a "successful" copy operation implies semantic validity.
The most reliable method is to open both the source and destination files in binary mode and compare them in chunks:
' Simplified binary comparison logic
Open sourcePath For Binary Access Read As #1
Open destPath For Binary Access Read As #2
Do While Not EOF(1)
Get #1, , buffer1
Get #2, , buffer2
If buffer1 <> buffer2 Then
' Handle mismatch
Exit Do
End If
Loop
Close #1
Close #2
The preferred restore path remains functionally the same, but the execution environment changes the risk profile. In a standalone VB6 executable, the application typically operates with the user's full filesystem permissions. In a VBA host (such as Excel or Access), the code may be subject to the host's security sandbox or "Trust Center" restrictions.
In VBA environments, you must verify that the host allows access to the target directory, as the host may trigger security warnings or block the FileSystemObject entirely if macros are restricted.
To refine this recommendation, please specify if the target files are stored in protected system directories (e.g., C:\Program Files) or user-profile directories, as this determines whether the application requires administrative elevation regardless of the copy method used.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.