Using Explorer's File Copy Dialog Box
For example to back up your documents and VB programs to a backup folder, you could use this code:
Dim lret As Long Dim fileop As SHFILEOPSTRUCT With fileop .hwnd = 0 .wFunc = FO_COPY .pFrom = "C:\Program Files\DevStudio\VB\My Programs" & _ vbNullChar & "C:\My Documents" & vbNullChar & vbNullChar .pTo = "c:\Backup of Documents" & vbNullChar & vbNullChar .lpszProgressTitle = "Please wait, backing up..." .fFlags = FOF_SIMPLEPROGRESS Or FOF_RENAMEONCOLLISION End With lret = SHFileOp(fileop) If result <> 0 Then ' Operation failed MsgBox Err.LastDllError 'Show the error returned from the API. Else If fileop.fAnyOperationsAborted <> 0 Then MsgBox "Operation Failed" End If End If
To send this directory to the recycle bin when it is too old, you could use this code:
Dim lret As Long Dim fileop As SHFILEOPSTRUCT With fileop .hwnd = 0 .wFunc = FO_DELETE .pFrom = "c:\Backup of Documents" & vbNullChar & vbNullChar .lpszProgressTitle = "Please wait, backing up..." .fFlags = FOF_SIMPLEPROGRESS Or FOF_ALLOWUNDO End With lret = SHFileOp(fileop) If result <> 0 Then ' Operation failed MsgBox Err.LastDllError 'Show the error returned from the API. Else If fileop.fAnyOperationsAborted <> 0 Then MsgBox "Operation Failed" End If End If
That just about wraps up the SHFileOperation function. Have an experiment and a play about with it, and before long you will have the wonderful progress dialog box seamlessly integrated into your application.
If you have any comments/suggestions/questions, please email me.
Page 3 of 3
This article was originally published on November 20, 2002