How-To... - Use the Animated Copy Functions in Windows 9x

When you copy, move or delete files, install certain applications(mostly Microsoft) you get to see the Animated Copy Functions. Yes, thats the one with the folder throwing stuff at the other folder :-) Using it in your Visual Basic program is quite simple, just a few API calls.

On a new form, stick a Command Button and give it the name of cmdCopy. In the General Declerations of the Form, paste the following code :-

'NOTE - These constants and some of the code is taken from
'KB article Q151799...
Option Explicit

'Copies the files specified in the pFrom member to the
'location specified in the pTo member...

Private Const FO_COPY = &H2&

'Deletes the files specified 'in pFrom (pTo is ignored.)
Private Const FO_DELETE = &H3&

'Moves the files specified in pFrom to the location
'specified in pTo...

Private Const FO_MOVE = &H1&

'Renames the files specified in pFrom...
Private Const FO_RENAME = &H4&

'Preserve Undo information...
Private Const FOF_ALLOWUNDO = &H40&

'Not currently implemented...
Private Const FOF_CONFIRMMOUSE = &H2&

'handle to the parent window for the progress dialog box...
Private Const FOF_CREATEPROGRESSDLG = &H0&

'Perform the operation on files only if a wildcard file
'(*.*) is specified...

Private Const FOF_FILESONLY = &H80& name

'The pTo member specifies multiple destination files...
'(one'for each source file) rather than one directory
'where all source files are to be deposited...

Private Const FOF_MULTIDESTFILES = &H1&

'Respond with Yes to All for any dialog box that is displayed...
Private Const FOF_NOCONFIRMATION = &H10&

'Does not confirm the creation of a new directory
'if the operation requires one to be created...

Private Const FOF_NOCONFIRMMKDIR = &H200&

'Give the file being operated on a new name in a
'move, copy, or rename operation if a file with the
'target name already exists...

Private Const FOF_RENAMEONCOLLISION = &H8&

'Does not display a progress dialog box...
Private Const FOF_SILENT = &H4&

'Displays a progress dialog box but does not show
'the file names...

Private Const FOF_SIMPLEPROGRESS = &H100&

'If FOF_RENAMEONCOLLISION is specified, the
hNameMappings member will be filled 'in if any
files were renamed.

Private Const FOF_WANTMAPPINGHANDLE = &H20&

'The SHFILOPSTRUCT is not double-word aligned. If no steps are
'taken, the last 3 variables will not be passed correctly. This
'has no impact unless the progress title needs to be changed...

Private Type SHFILEOPSTRUCT

hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String

End Type

'CopyMemory sub...
Private Declare Sub CopyMemory Lib "KERNEL32" _
Alias "RtlMoveMemory" (hpvDest As Any, _
hpvSource As Any, ByVal cbCopy As Long)

'SHFileOperation function...
Private Declare Function SHFileOperation Lib "Shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As Any) As Long

cmdCopy Code

Dim result As Long
Dim lenFileop As Long
Dim foBuf() As Byte
Dim fileop As SHFILEOPSTRUCT

lenFileop = LenB(fileop) ' double word alignment increase...
ReDim foBuf(1 To lenFileop) ' the size of the structure...

With fileop
.hwnd = Me.hwnd

'This means that the Function will be Copy
'Look in the General Declerations for more
'constants to use with the Copy Box...

.wFunc = FO_COPY

'BELOW: The files to be copied, you can use wild cards, or
'just specify each file. Each file to be copied must be seperated
'by vbNullChar and finally 2 of them at the end...

.pFrom = App.Path & "\readme.html" _
& vbNullChar _
& App.Path & "\readme.doc" _
& vbNullChar _
& App.Path & "\readme.txt" _
& vbNullChar _
& vbNullChar

'Where the files will end up - you can specify a
'directory and it will be created(use a flag{see below}
'if you wish the directory to be created without asking...

.pTo = "C:\" .

'BELOW: Which 'flags' you wish to use - these are defined by the
'Constants in the General Declerations - you can hide the progress bar,
'or do a few more things...

.fFlags = FOF_CREATEPROGRESSDLG

'Give the dialog box a title...
.lpszProgressTitle = "VB HowTo Copy Example " & _
vbNullChar _
& vbNullChar

End With

'Call the CopyMemory procedure and copy the
'structure into a byte array(??)...

Call CopyMemory(foBuf(1), fileop, lenFileop)

Call CopyMemory(foBuf(19), foBuf(21), 12)

'This is used for Error Handling - 0 means everything was
'fine, any other number means their was an error...

result = SHFileOperation(foBuf(1))

'ERROR HANDLER....
If result <> 0 Then ' Operation failed

'Show the error returned from 'the API...
MsgBox Err.LastDllError

Else

If fileop.fAnyOperationsAborted <> 0 Then

MsgBox "Operation Failed"

End If

End If

Now, run the program. Click Copy and depending on the speed of your computer, you may see the progress box(screenshot above). If not, click Copy again and when it asks you to overwrite, click YES, then you will see the progress box behind the message box.

You can download this example by clicking here


This site is Copyright © 1999 of David Cowan. VB How-To, Dodgesoft and DodgeSoft Productions is Copyright © 1999 of David Cowan. Source-code and programs are copyright of their respective owners. For a messageboard, click here