RmDir Procedure

   
RmDir Procedure

Class

Microsoft.VisualBasic.FileSystem

Syntax

 RmDir(   path)   
path (required; String)

The path of the folder to be removed

Description

Removes a folder

Rules at a Glance

  • 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.

  • path can be a fully qualified, relative, or UNC pathname.

  • If the folder contains files or other folders, RmDir will generate runtime error 75, "Path/File access error."

Example

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 recursively calling itself 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(  )       End If       If strFile <> "" And _           strFile <> "." And strFile <> ".." Then           If Not blnLowerLevel Then               blnLowerLevel = True               If MsgBox("Delete subdirectories of " & _              strFolder & "?", _              vbQuestion BitOr vbOKCancel, _              "Confirm Directory Deletion") _              = vbCancel Then Exit Sub           End If           RemoveFolder(strFolder & "\" & strFile)           blnRepeated = False       End If   Loop While strFile <> ""   RmDir(strFolder) End Sub 

Programming Tips and Gotchas

  • Use the Kill procedure to delete any remaining files from the folder prior to removing the folder.

  • The effects of using Kill and RmDir are irreversible, since these statements do not move deleted files to the Recycle Bin.

See Also

MkDir Procedure

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net