fortypoundhead.com

ShellFileCopy with VB6

Posted On 2012-11-25 by dwirch
Keywords:
Tags: Visual Basic 6 Scripting General Windows
Views: 1352


Copying a file with VB6 is pretty straightforward. However, giving feedback to the user during the copy process has historically been a challenge.

By leveraging the Windows API, we can utilize the same functions that Windows itself uses to copy files, and at the same time make use of the built in copy status dialog. Another VB6 post, by request.

Success or failure of the copy process will be returned as well, with a true/false value.

Example:
dim bSuccess as boolean
bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt")


Option Explicit

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 Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private Const FO_COPY = &H2

Public Function ShellFileCopy(src As String, dest As String, _
Optional NoConfirm As Boolean = False) As Boolean

Dim WinType_SFO As SHFILEOPSTRUCT
Dim lRet As Long
Dim lflags As Long

lflags = FOF_ALLOWUNDO
If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION
With WinType_SFO
.wFunc = FO_COPY
.pFrom = src
.pTo = dest
.fFlags = lflags
End With

lRet = SHFileOperation(WinType_SFO)
ShellFileCopy = (lRet = 0)

End Function


About the Author

dwirch has posted a total of 172 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.147.241.117

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

Job Spammer: Sathya Narayana
dwirch posted on March 15, 2017 at about 7:18 in Spammers

Job Spammer: Manish Kumar
dwirch posted on March 14, 2017 at about 17:44 in Spammers

Site Migration Complete
dwirch posted on March 12, 2017 at about 11:10 in Site News

Site Migration Complete
dwirch posted on March 11, 2017 at about 0:35 in Site News

Job Spammer: Jason Weinstein
dwirch posted on March 10, 2017 at about 14:00 in Spammers

Job Spammer: Ryan Wilson
dwirch posted on March 8, 2017 at about 17:00 in Spammers