Microsoft KB Archive/165919

= How to use the Windows 95/98/Me Copy and Recycle functions in VB =

Article ID: 165919

Article Last Modified on 8/30/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q165919



SUMMARY
Through the Microsoft Windows API, you can copy, move, rename, or delete a file system object by using the SHFileOperation API function that Shell32.dll exports. This article describes how to copy a list of files into a named folder and then move them to the Recycle Bin.

You can use the SHFileOperation API function to move, to rename, or to delete a file system object based on the flags that are passed to this function. You can also set the title of the progress dialog box by assigning a custom string to the lpszProgressTitle member of the SHFILEOPSSTRUCT structure. However, to do this requires some special handling. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

151799 How to use the animated copy functions in Windows 95/98/Me

In Visual Basic 6.0, you can use the FileSystemObject object to copy, to move, to rename, and to delete files. However, you cannot use the FileSystemObject object to move files to the Recycle Bin.



MORE INFORMATION
The following example creates a single form Microsoft Visual Basic project that uses the SHFileOperation API function.

Step-by-step example
 Start a new Standard EXE project. By default, a form that is named Form1 is created. Add two CommandButton objects to Form1. Name them Command1 and Command2. On the Project menu, click Add Module to add a module to the project.  Add the following code to Module1. This code is made up of the function, the type, and the constant declarations

Note This sample uses VB.HLP and VB.CNT that are installed with a complete installation of Microsoft Visual Basic. If you do not have these files, you can change this sample to copy other existing files. Public Const FO_MOVE As Long = &H1 Public Const FO_COPY As Long = &H2 Public Const FO_DELETE As Long = &H3 Public Const FO_RENAME As Long = &H4

Public Const FOF_MULTIDESTFILES As Long = &H1 Public Const FOF_CONFIRMMOUSE As Long = &H2 Public Const FOF_SILENT As Long = &H4 Public Const FOF_RENAMEONCOLLISION As Long = &H8 Public Const FOF_NOCONFIRMATION As Long = &H10 Public Const FOF_WANTMAPPINGHANDLE As Long = &H20 Public Const FOF_CREATEPROGRESSDLG As Long = &H0 Public Const FOF_ALLOWUNDO As Long = &H40 Public Const FOF_FILESONLY As Long = &H80 Public Const FOF_SIMPLEPROGRESS As Long = &H100 Public Const FOF_NOCONFIRMMKDIR As Long = &H200

Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Long fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String End Type

Declare Function SHFileOperation Lib "Shell32.dll" _ Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long   Add the following code behind Form1. Private Sub Command1_Click Dim result As Long Dim fileop As SHFILEOPSTRUCT With fileop .hwnd = Me.hwnd

.wFunc = FO_COPY

'The files to copy. They are separated by nulls and are terminated by using 'two nulls. The files VB.HLP and VB.CNT are installed by performing a complete 'installation of Microsoft Visual Basic. If you do not have 'these files, you can change this sample to point to existing 'files.

.pFrom = "C:\PROGRAM FILES\MICROSOFT VISUAL BASIC\VB.HLP" & _ vbNullChar & _ "C:\PROGRAM FILES\MICROSOFT VISUAL BASIC\VB.CNT" & _ vbNullChar & vbNullChar

'or to copy all files use this line '.pFrom = "C:\*.*" & vbNullChar & vbNullChar

'The directory or the filenames to copy into. They are terminated by using 'two nulls. .pTo = "C:\testfolder\" & vbNullChar & vbNullChar

.fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY End With

result = SHFileOperation(fileop)

If result <> 0 Then       'Operation failed MsgBox result,, "Copy Operation Failed" Else If fileop.fAnyOperationsAborted <> 0 Then MsgBox fileop.fAnyOperationsAborted,, "Operation Aborted" End If        End If      End Sub

Private Sub Command2_Click Dim DelFileOp As SHFILEOPSTRUCT Dim result As Long With DelFileOp .hwnd = Me.hwnd .wFunc = FO_DELETE 'Delete the files that you just moved to the C:\TestFolder folder. 'The files VB.HLP and VB.CNT are installed by a complete 'installation of Microsoft Visual Basic. If you do not have 'these files, you can change this sample to point to existing 'files.

.pFrom = "C:\testfolder\vb.hlp" & vbNullChar & _ "c:\testfolder\vb.cnt" & vbNullChar & vbNullChar 'Allow undo--in other words, place the files into the Recycle 'Bin .fFlags = FOF_ALLOWUNDO End With result = SHFileOperation(DelFileOp) If result <> 0 Then       'Operation failed MsgBox result,, "Delete Operation Failed" Else If DelFileOp.fAnyOperationsAborted <> 0 Then MsgBox DelFileOp.fAnyOperationsAborted,, "Operation Aborted" End If        End If      End Sub

Private Sub Form_Load Command1.Caption = "Copy Test" Command2.Caption = "Recycle Test" End Sub  On the Run menu, click Start, or press F5 to run the application. Click Copy Test. If the destination folder does not exist, a prompt to create the folder appears. The copy animation appears if the operation requires sufficient time to display it.</li> Click 'Recycle Test'. A confirmation message about the deleted files appears.</li></ol>

Additional query words: Microsoft Win32 SDK SHFileOperation SHFILEOPSTRUCT

Keywords: kbhowto kbapi KB165919

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.