SHFileOperation Function

Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As Byte) As Long

Platforms

Description & Usage

SHFileOperation copies, moves, renames, or deletes an object in the file system. Instead of performing the action silently as regular file API functions do, SHFileOperation uses the dialog box prompts of the shell. This function is also the proper way to send one or more files to the Recycle Bin instead of deleting them outright.

Return Value

If an error occured, the function returns a non-zero error code. If successful, the function returns 0.

Visual Basic-Specific Issues

Although lpFileOp is technically a SHFILEOPSTRUCT structure passed to SHFileOperation, a byte array must be used. This is because of a byte alignment quirk in Visual Basic that results in misplacing some data in the structure. Note the code used in the example to fix the problem. For more information about the reason for this, consult the SHFILEOPSTRUCT structure's page.

Parameters

lpFileOp
A SHFILEOPSTRUCT structure, copied properly into a byte array, that specifies the file operation to perform. It may also receive some feedback from the function.

Example

When the user clicks button cmdDelete, send all the files inside C:\Stuff\ to the Recycle Bin. Prompt the user as necessary. To use this example, place a command button named cmdDelete on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public 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
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
	(Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
	(lpFileOp As Byte) As Long
Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
Public Const FOF_FILESONLY = &H80

' *** Place the following code inside the form window. ***

Private Sub cmdDelete_Click()
	Dim fos As SHFILEOPSTRUCT  ' structure to pass to the function
	Dim sa(1 To 32) As Byte    ' byte array to make structure properly sized
	Dim retval As Long         ' return value
	
	' Load the proper parameters into the structure.
	With fos
		' The window invoking the file operation.
		.hwnd = Form1.hWnd
		' Delete the specified files.
		.wFunc = FO_DELETE
		' The list of files to delete.
		.pFrom = "C:\Stuff\*.*" & vbNullChar & vbNullChar
		' Target information isn't needed for deletion of files.
		.pTo = vbNullChar & vbNullChar
		' Allow Undo (i.e., send to Recycle Bin on delete), and do not delete subfolders.
		.fFlags = FOF_ALLOWUNDO Or FOF_FILESONLY
		' The rest of the structure isn't needed for this example.
		.fAnyOperationsAborted = 0
		.hNameMappings = 0
		.lpszProgressTitle = vbNullChar
	End With
	
	' Transfer the contents of the structure object into the byte
	' array in order to compensate for a byte alignment problem.
	CopyMemory sa(1), fos, LenB(fos)
	CopyMemory sa(19), sa(21), 12
	
	' Send those files to the Recycle Bin.
	retval = SHFileOperation(sa(1))
	
	' Although not necessary for this example, transfer the (possibly changed)
	' byte array back into the structure.  This would be necessary in case SHFileOperation might
	' have placed some feedback information into the structure passed to it.
	CopyMemory sa(21), sa(19), 12
	CopyMemory fos, sa(1), Len(fos)
End Sub

Category

Shell

Back to the Function list.
Back to the Reference section.


Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/shfileoperation.html