ID Number: Q77315
1.00
WINDOWS
Summary:
Visual Basic does not have a command to copy a disk file such as
the MS-DOS COPY command. However, you can write the necessary code
to copy a file. Two examples of copying a file are provided in
this article.
This information applies to Microsoft Visual Basic programming system
version 1.0 for Windows.
More Information:
The following Visual Basic sample subprograms, CopyFile1 and
CopyFile2, provide two different ways to copy a disk file in a way
similar to the MS-DOS COPY command. The first example uses only Visual
Basic code, while the second example includes Window API functions.
CopyFile2 runs faster than CopyFile1, especially for large files (up
to twice as fast).
Subprogram: CopyFile1
---------------------
Sub CopyFile1 (ByVal Source As String, ByVal Destination As String)
Dim Index As Integer, NumBlocks As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData As String
Const BlockSize = 32768
'Source and Destination are strings containing filenames:
Open Source For Binary Access Read As #1
' Opening then immediately closing the destination file with
' "For Output" access erases the file if it exists (which is
' necessary in case the copied Source file is shorter than the
' existing Destination file, which would leave some of the old
' file's characters at the end of the new Destination file).
' You can use this technique to erase the Destination file in place
' of the Kill statement to avoid a Kill statement error if the
' Destination file doesn't exist:
Open Destination For Output As #2
Close #2
Open Destination For Binary As #2
FileLength = LOF(1)
NumBlocks = FileLength \ BlockSize
LeftOver = FileLength Mod BlockSize
FileData = String$(LeftOver, 32)
Get #1, , FileData
Put #2, , FileData
FileData = String$(BlockSize, 32)
For Index = 1 To NumBlocks
Get #1, , FileData
Put #2, , FileData
Next Index
Close #1, #2
End Sub
Subprogram: CopyFile2
---------------------
Note that CopyFile2 (below) copies files faster than CopyFile1
(above). Because CopyFile2 uses several API functions, you must
include the Visual Basic Declare statements shown below. Place these
declarations in the global file or in the (general) (declarations)
section of a form or module file that contains the CopyFile2
subprogram:
DefInt A-Z
' All Declare statements must be on one line when added to a program:
Declare Function fWrite Lib "kernel" Alias "_lwrite" (ByVal hFile,
ByVal lpBuff As Long, ByVal nBuff)
Declare Function fRead Lib "kernel" Alias "_lread" (ByVal hFile,
ByVal lpBuff As Long, ByVal nBuff)
Declare Function GLobalAlloc Lib "kernel" (ByVal wFlags, ByVal nBuff
As Long)
Declare Function GLobalFree Lib "kernel" (ByVal hMem)
Declare Function GLobalLock Lib "kernel" (ByVal hMem) As Long
Declare Function GLobalUnlock Lib "kernel" (ByVal hMem)
Sub CopyFile2 (ByVal Source As String, ByVal Destination As String)
Dim lpBuff As Long
Dim DestFile, SourceFile As Integer
Dim DestDOS, SourceDOS As Integer
Dim ApiErr, AmtRead As Integer
Dim hMem As Integer
Const nBuff = 32767
Const wFlags = &H20
hMem = GLobalAlloc(wFlags, nBuff)
lpBuff = GLobalLock(hMem)
DestFile = FreeFile
Open Destination For Output As #DestFile Len = 1
SourceFile = FreeFile
Open Source For Input As #SourceFile Len = 1
DestDOS = FileAttr(DestFile, 2)
SourceDOS = FileAttr(SourceFile, 2)
Do
AmtRead = fRead(SourceDOS, ByVal lpBuff, nBuff)
ApiErr = fWrite(DestDOS, ByVal lpBuff, AmtRead)
Loop Until AmtRead = 0
Close #SourceFile, #DestFile
lpBuff = GLobalUnlock(hMem)
hMem = GLobalFree(hMem)
End Sub
Additional reference words: 1.00