RmDir Statement |
No
RmDir path
path
Use: Required
Data Type: String
The path of the folder to be removed.
Removes a folder.
You may include a drive letter in path ; if you don't specify a drive letter, the folder is assumed to be on the current drive.
If the folder contains files or other folders, RmDir will generate runtime error 75, "Path/File access error."
The following subroutine deletes all the files in a folder and removes its subfolders. If those contain files or folders, it deletes those too by calling itself recursively until all child folders and their files are removed.
Private Sub RemoveFolder(ByVal strFolder As String) Static blnLowerLevel As Boolean ' A recursive call - no ' need to prompt user Dim blnRepeated As Boolean ' Use Dir state info on ' repeated calls Dim strFile As String ' File/Directory contained in ' strFolder ' Delete all files Do strFile = Dir(strFolder & "*.*", _ vbNormal Or vbHidden Or vbSystem) If strFile <> "" Then If Not blnLowerLevel Then If MsgBox("Delete files in directory " & _ strFolder & "?", _ vbQuestion Or vbOKCancel, _ "Confirm File Deletion") _ = vbCancel Then Exit Sub End If strFile = strFolder & "" & strFile Kill strFile End If Loop While strFile <> "" ' Delete all directories Do If Not blnRepeated Then strFile = Dir(strFolder & "*.*", vbDirectory) blnRepeated = True Else strFile = Dir(, vbDirectory) End If If strFile <> "" And _ strFile <> "." And strFile <> ".." Then If Not blnLowerLevel Then blnLowerLevel = True If MsgBox("Delete subdirectories of " & _ strFolder & "?", _ vbQuestion Or vbOKCancel, _ "Confirm Directory Deletion") _ = vbCancel Then Exit Sub End If RemoveFolder strFolder & "" & strFile blnRepeated = False End If Loop While strFile <> "" RmDir strFolder End Sub
Use the Kill statement to delete any remaining files from the folder prior to removing the folder.
To remove folders, you can call the Dir function recursively to navigate downward into a folder's subfolders. Note that because it saves state information between invocations, the documentation incorrectly indicates that the Dir function can't be called recursively. The previous example indicates how this might be done.
The effects of using Kill and RmDir are irreversible, since these statements don't move deleted files to the Recycle Bin.
Visual Basic Version 6 introduces the File System object model, which contains Folders and Folder objects and gives much greater control and flexibility that the intrinsic MkDir and RmDir statements. Removing a folder using the FileSystemObject.DeleteFolder method is similar to deleting a folder using the Windows Explorer: i.e., all files, subfolders, and their contents are removed.