Professional Documents
Culture Documents
This page describes a procedure named EmptyFolder which you can use
to delete the entire contents of a folder, but leave the folder intact. This
code requires a reference to the Microsoft Scripting Runtime library. In
VBA, go to the Tools menu, choose References, and scroll down to and
check "Microsoft Scripting Runtime".
The entire code module is shown below. You may download a bas module
file.
Option Explicit
Option Compare Text
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
' modEmptyFolder
' By Chip Pearson, www.cpearson.com , chip@cpearson.com
'
' This contains the public EmptyFolder procedure and Private
supporting procedures.
' The EmptyFolder procedure will delete the contents of the
specified folder, but
' will not delete the folder itself. You can specify whether the
contents of the
' folder should go to the Recycle Bin (default behavior) or should
be Killed.
'
' This module needs a reference to the Microsoft Scripting Runtime
library.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Used for RecycleFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
End Function
End Sub
End Sub
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''
' Ensure we weren't passed a empty string
'''''''''''''''''''''''''''''''''''''''''''''''''''''
If sFile = vbNullString Then
MsgBox "The sFile parameter is an empty string."
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''
' Check if the file or folder exists.
'''''''''''''''''''''''''''''''''''''''''''''''''''''
If Dir(sFile, vbNormal + vbArchive + vbSystem _
+ vbHidden + vbDirectory) = vbNullString Then
MsgBox "The file '" & sFile & "' does not exist.",
vbOKOnly, "Recycle File"
Exit Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''
' Ensure that sFile isn't a network file name. You
' can't recycle network files.
''''''''''''''''''''''''''''''''''''''''''''''''''''
If PathIsNetworkPath(sFile) <> 0 Then
MsgBox "The file name '" & sFile & "' is a network name" &
_
" or a exists on a mapped drive." & vbCrLf & _
"Network files cannot be sent to the Recycle Bin.",
_
vbOKOnly, "Recycle File"
Exit Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''
' Ensure we have a fully qualified file name. That
' means it has a drive specification character (:).
''''''''''''''''''''''''''''''''''''''''''''''''''''
If InStr(1, sFile, ":") = 0 Then
MsgBox "The file name '" & sFile & _
"' is not a fully qualified file name." & vbCrLf &
_
"The file name must include the drive name and
folder path.", _
vbOKOnly, "Recycle File"
Exit Sub
End If
With FileOperation
.wFunc = FO_DELETE
.pFrom = sFile
.fFlags = FOF_ALLOWUNDO
'
' OR if you want to suppress the "Do You want
' to delete the file" message, use
'
.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
End With
lReturn = SHFileOperation(FileOperation)
End Sub
End Function