Empty A Folder
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 function declaration is as follows: Public Function EmptyFolder(FullFolderName As String, _ Optional NoRecycle As Boolean = False) As Boolean FullFolderName is the name of the folder to empty. This must be a fully-qualified folder name on the a local drive. By design, EmptyFolder does not work with mapped drives or network folders. This restriction is due to the fact that you cannot send a folder or file on a mapped drive or network drive to the Recycle Bin. Attempting to Recycle a folder or file on a mapped or network drive permanently deletes the folder or file. In order to support Recycling a file, the EmptyFolder procedure requires that FullFolderName be on a local drive. NoRecycle indicates that the contents of FullFolderName be permanently deleted, not sent to the Recycle Bin. If NoRecycle is omitted or False, the contents of FullFolderName will be sent to the Windows Recycle Bin. If NoRecycle is True, the contents of FullFolderName are permanently destroyed. There is no way to get them back. To Recycle a single file or folder, see Sending A FIle Or Folder To The Recycle Bin. 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 Private Declare Function PathIsNetworkPath Lib "shlwapi.dll" _ Alias "PathIsNetworkPathA" ( _ ByVal pszPath As String) As Long Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40 Private Const FOF_NOCONFIRMATION = &H10 Private Type SHFILEOPSTRUCT HWnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Boolean hNameMappings As Long lpszProgressTitle As String End Type Public Function EmptyFolder(FullFolderName As String, _ Optional NoRecycle As Boolean = False) As Boolean ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' EmptyFolder ' This function completely deletes all the contents of FullFolderName, ' but does not delete FullFolderName itself. FullFolderName must be ' a fully-qualified folder name on a local drive. It may not be a ' folder on a mapped drive or a network share. ' If NoRecycle is omiited for False, the contents of FullFolderName ' are sent to the Windows Recycle Bin. If NoRecycle is True, the ' contents of FullFolderName are killed, and there is no way to undo ' the operation or restore the files and folder. The contents are ' gone forever. While killing a file or folder is much riskier because ' it is no undoable, killing is much faster than recycling. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim FSO As Scripting.FileSystemObject Dim FolderObj As Scripting.Folder If IsLocalFolder(FullFolderName) = False Then EmptyFolder = False Exit Function End If Set FSO = New Scripting.FileSystemObject On Error Resume Next Set FolderObj = FSO.GetFolder(FullFolderName) If Err.Number <> 0 Then EmptyFolder = False Exit Function End If EmptyOneFolder FSO:=FSO, FolderObj:=FolderObj, NoRecycle:=NoRecycle EmptyFolder = True End Function Private Sub EmptyOneFolder(FSO As Scripting.FileSystemObject, _ FolderObj As Scripting.Folder, NoRecycle As Boolean) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' EmptyOneFolder ' This empties one folder. It calls itself recursively for each ' subfolder. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim SF As Scripting.Folder Dim F As Scripting.File For Each F In FolderObj.Files KillFile F.Path, NoRecycle Next F For Each SF In FolderObj.SubFolders EmptyOneFolder FSO:=FSO, FolderObj:=SF, NoRecycle:=NoRecycle KillSubFolder SF.Path, NoRecycle Next SF End Sub Private Sub KillSubFolder(FolderName As String, NoRecycle As Boolean) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' KillSubFolder ' This kills or recycles the folder named by FolderName. If NoRecycle ' is False, the folder is sent to the Recycle Bin. If NoRecycle is ' True, the folder is RMand cannot be restored. Note that RmDir ' requires that the folder be empty. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If NoRecycle = True Then RmDir FolderName Else RecycleFile sFile:=FolderName End If End Sub Private Sub KillFile(FileName As String, NoRecycle As Boolean) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' KillFile ' This kills or recycles the file named by FileName. If NoRecycle ' is False, the file is sent to the Recycle Bin. If NoRecycle is ' True, the file is killed and cannot be restored. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If NoRecycle = True Then Kill FileName Else RecycleFile sFile:=FileName End If End Sub Private Sub RecycleFile(sFile As String) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' RecycleFile ' This procedure send the file or folder specified in sFile to the Recycle Bin. ' sFile must be a fully qualified file name, and must refer to a file on a ' local drive. Files on a network or mapped drive cannot be recycled. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim FileOperation As SHFILEOPSTRUCT Dim lReturn As Long ''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 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 Private Function IsLocalFolder(FolderName As String) As Boolean '''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' IsLocalFolder ' This returns True if FolderName is the name of a ' local folder, or False if FolderName does not ' exist or is on a network share or mapped drive. '''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim FSO As Scripting.FileSystemObject Dim DriveObj As Scripting.Drive Dim FolderObj As Scripting.Folder Dim Pos As Integer Set FSO = New Scripting.FileSystemObject '''''''''''''''''''''''''''''''''''''''''' ' First ensure that we have a fully ' qualified folder name. This is tested ' by looking for the ":" character. '''''''''''''''''''''''''''''''''''''''''' If InStr(1, FolderName, ":", vbTextCompare) = 0 Then IsLocalFolder = False Exit Function End If Set FSO = New Scripting.FileSystemObject On Error Resume Next Err.Clear Set FolderObj = FSO.GetFolder(FolderName) If Err.Number <> 0 Then IsLocalFolder = False Exit Function End If Set DriveObj = FolderObj.Drive If DriveObj.ShareName = vbNullString Then IsLocalFolder = True Else IsLocalFolder = False End If End Function
|
|
|