Deleting Folders with DOS Command
If we can create a folder then we must be able to remove the folder too. But, RmDir() Command usage is not as common as the MkDir() Command. We create new folders or sub-folders to organize files into them so that we can easily locate them when needed. The removal of those folders become necessary only when need arises for relocation/removal of those files and release the disk space.
We can run RmDir() command directly from the Immediate Window (Debug Window) as shown below, to learn it’s usage:
This command have a safety-check built into it. When you run the above command it will check whether the folder is totally empty of any sub-folders or files. If the Command execution was successful then it will not show any message, otherwise it will one of the following two messages:
- If the Path specified is not correct then it will show ‘Path Not found’ Error Message.
- If the folder is not empty then the message ‘Path/File access error’ is displayed.
Through Windows explorer we can remove a folder with all it’s sub-folders and files in one clean sweep. If this is done by mistake then we can always restore them from the Recyclebin also, before emptying it.
Network Folders or Files deleted through Windows Explorer are not transferred to Recyclebin. If you have access rights to delete Network Folders/Files then you must approach the Network Administrator to restore the Folder/File from the latest LAN Backup. Normally, some users may be allowed to create folders but access rights to delete folder is kept with the Network Administrator, as a safety measure.
Let us write a small function FolderDeletion() to run the RmDir() DOS Command with proper validation checks so that you can add this to your other common functions library. The function will have the following validation checks before the folder is physically removed:
- It checks whether the folder name passed to the function exists, if not displays a message in that effect and aborts the program.
- If the folder exists then asks for confirmation from the user to delete the folder.
- if the user’s response is not to delete the folder then aborts the program, otherwise attempt to delete the folder.
- If the delete action fails then the folder has sub-folders or files in it, abort the program, otherwise delete the folder and show a message.
The VBA Code is given below:
Public Function DeleteFolder(ByVal strFolder As String) On Error Resume Next If Len(Dir(strFolder, vbDirectory)) > 0 Then 'Folder exists, ask for permission to delete the folder If (MsgBox("Deleting Folder: '" & strFolder & "', Proceed...?", vbOKCancel + vbDefaultButton2 + vbQuestion, "DeleteFolder()") = vbNo) Then 'User says not to delete the folder, exit program GoTo DeleteFolder_Exit Else 'Delete Folder RmDir strFolder If Err = 75 Then 'folder is not empty, have sub-folders or files MsgBox "Folder: '" & strFolder & "' is not empty, cannot be removed." GoTo DeleteFolder_Exit Else MsgBox "Folder: '" & strFolder & "' deleted." GoTo DeleteFolder_Exit End If End If Else MsgBox "Folder: '" & strFolder & "' Not found." End If DeleteFolder_Exit: On Error GoTo 0 End Function
If you want something different to get the same work done then we can use VB Script in Microsoft Access to do that. VB Script mostly used in Web Pages for Server Side actions. VB Script uses FileSystemObject to manage Drives, Folders & Files. We have used it for creating Text, Word and Excel Files before.
You can find those examples in the following links:
First let us write a VB Script Function to create a Folder - C:\MyProjects.
Public Function FolderCreation(ByVal strFolder As String) Dim FSysObj, fldr On Error Resume Next 'arrange to capture the error so that it can be check 'Create the File System Object Set FSysObj = CreateObject("Scripting.FileSystemObject") 'Call the Create Folder Method of the File System Object with Folder Path as parameter Set fldr = FSysObj.CreateFolder(strFolder) 'if this action ended up with error code 58 then the folder already exists If Err = 58 Then MsgBox "Folder: '" & strFolder & "' already exists." GoTo FolderCreation_Exit Else MsgBox "Folder: " & strFolder & " created successfully." End If FolderCreation_Exit: On Error GoTo 0 End Function
Copy and paste the above function into the Standard Module of your database. You can try the function by calling it from the Debug Window with a folder name as shown below:
After the sample run, open Windows Explorer and check for the folder name c:\MyProjects. The following VB Script Function FolderDeletion() can be used for removing a folder:
Public Function FolderDeletion(ByVal strFolder As String) Dim FSysObj, fldr On Error Resume Next Set FSysObj = CreateObject("Scripting.FileSystemObject") Set fldr = FSysObj.GetFolder(strFolder) If Err = 76 Then MsgBox "Folder: '" & strFolder & "' No found!" Else If MsgBox("Delete Folder: '" & strFolder & "' Proceed...?", vbOKCancel + vbDefaultButton2 + vbQuestion, "FolderDeletion()") = vbNo Then GoTo FolderDeletion_Exit Else fldr.Delete 'call the Delete Method of the Folder Object MsgBox "Folder: '" & strFolder & "' Deleted." End If End If FolderDeletion_Exit: On Error GoTo 0 End Function
Sample Run from Debug Window:
Private Sub cmdRun_Click() FolderDeletion txtFolderPath End Sub