Search Tools Links Login

Deleting Directory with all the Subdirectories


Visual Basic 6, or VB Classic

Delete a folder, with all it's subdirectories.

Original Author: Vlad Azarkhin

Inputs

The folder name, for example:
"c:My documentsstuff" will delete the "stuff" folder.

Assumptions

Simple recursion.

Side Effects

System and hidden files will not be deleted, so the directory won't be deleted either. However You cen rewrite the function a little to work with hidden file.

Code

Public Sub CleanAllPath(sPath As String)
Dim sName As String
Dim sFullName As String
' Array used for holding the directories,
' however collection may be used as well
Dim Dirs() As String
Dim DirsNo As Integer
Dim i As Integer
If Not Right(sPath, 1) = "" Then
sPath = sPath & ""
End If
' clean all files in the directory
sName = Dir(sPath & "*.*")
While Len(sName) > 0
sFullName = sPath & sName
SetAttr sFullName, vbNormal
Kill sFullName
sName = Dir
Wend

sName = Dir(sPath & "*.*", vbHidden)
While Len(sName) > 0
sFullName = sPath & sName
SetAttr sFullName, vbNormal
Kill sFullName
sName = Dir
Wend

' read all the directories into array
DirsNo = 0
sName = Dir(sPath, vbDirectory)
While Len(sName) > 0
If sName <> "." And sName <> ".." Then
  DirsNo = DirsNo + 1
  ReDim Preserve Dirs(DirsNo) As String
  Dirs(DirsNo - 1) = sName
End If
sName = Dir
Wend
For i = 0 To DirsNo - 1
CleanAllPath (sPath & Dirs(i) & "")
RmDir sPath & Dirs(i)
Next
  
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 109 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.