VB icon

A Cool Directory Map

Email
Submitted on: 1/7/2015 4:55:00 PM
By: Coolwick (from psc cd)  
Level: Advanced
User Rating: By 18 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 1313
 
     This simple sub routine is used to map out a directory tree starting with any given path. Can be easily modified to perform any task that requires scanning folders. THIS IS RECURSIVE!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A Cool Directory Map
' Description:This simple sub routine is used to map out a directory tree starting with any given path. Can be easily modified to perform any task that requires scanning folders. 
THIS IS RECURSIVE!
' By: Coolwick (from psc cd)
'
' Inputs:Full starting path
ex: C:\windowsex: \\computername\share\
'
' Assumes:This routine is generic. As it stands it will just show the directory structure in the immediate window. However, it can be modified easily for file search.
'**************************************

Private Sub CommandButton1_Click()
 Call DirMap("C:\Windows\")
 'Must have "\" at the end of the path
End Sub
Sub DirMap(ByVal Path As String)
On Error Resume Next
 Dim i, j, x As Integer 'All used as counters
 Dim Fname(), CurrentFolder, Temp As String
 Temp = Path
 If Dir(Temp, vbDirectory) = "" Then Exit Sub 'if there arent any sub directories the exit
 CurrentFolder = Dir(Temp, vbDirectory)
 'First get number of folders (Stored in i)
 Do While CurrentFolder <> ""
 If GetAttr(Temp & CurrentFolder) = vbDirectory Then
If CurrentFolder <> "." And CurrentFolder <> ".." Then
i = i + 1
End If
 End If
 CurrentFolder = Dir
 Loop
 ReDim Fname(i) 'Redim the array with number of folders
 'now store the folder names
 CurrentFolder = Dir(Temp, vbDirectory)
 Do While CurrentFolder <> ""
 If GetAttr(Temp & CurrentFolder) = vbDirectory Then
If CurrentFolder <> "." And CurrentFolder <> ".." Then
j = j + 1
Fname(j) = CurrentFolder
Debug.Print Temp & Fname(j)
End If
 End If
 CurrentFolder = Dir
 Loop
 ' For each folder check to see there are sub folders
 For x = 1 To i
 Call DirMap(Temp & Fname(x) & "\")
 Next
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.