Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
How to get animating window when copying file
This is an example how to get animating window (just like in Windows Explorer) when copying file. With a little workaround, you can make a class to Copy, Delete, Move or Rename file(s). For this, check on SHFILEOPSTRUCT documentation.
To copy file, just call FileCopy function from your code:
FileCopy "C:\Test\Sample.exe", "C:\Program Files\"
Put this code in module:
Option Explicit
Private Const FO_COPY = &H2&
Private Const DRIVE_NO_ROOT_DIR = 1
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
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (lpFileOp As Any) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias _
"GetDriveTypeA" (ByVal nDrive As String) As Long
Public Sub FileCopy(FilePath As String, DestinationPath As String)
Dim FoBuf() As Byte
Dim shFO As SHFILEOPSTRUCT
On Error GoTo ErrorHandler
If Dir(FilePath) = "" Then
MsgBox "File doesn't exists!"
Exit Sub
End If
If GetDriveType(DestinationPath) = DRIVE_NO_ROOT_DIR Then
If Dir(DestinationPath, vbDirectory) = "" Then
MsgBox "Destination directory doesn't exists!"
Exit Sub
End If
End If
ReDim FoBuf(1 To LenB(shFO)) ' the size of the structure.
With shFO
.wFunc = FO_COPY
.pFrom = FilePath
.pTo = DestinationPath
End With
' Copy the structure into a byte array
Call CopyMemory(FoBuf(1), shFO, LenB(shFO))
' Next we move the last 12 bytes by 2 to byte align the data
Call CopyMemory(FoBuf(19), FoBuf(21), 12)
If SHFileOperation(FoBuf(1)) <> 0 Or shFO.fAnyOperationsAborted <> 0 Then
GoTo ErrorHandler
End If
Exit Sub
ErrorHandler:
If Err.Number = 52 Then
MsgBox "No disk found in drive a:"
Else
MsgBox "Copying unsuccessful!"
End If
End Sub |
____________________________
If you find the answer helpful, please mark this topic as solved.
|