VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A Cool Directory Map

by Coolwick (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (18 Votes)

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!

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.

Rate A Cool Directory Map

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

Download this snippet    Add to My Saved Code

A Cool Directory Map Comments

No comments have been posted about A Cool Directory Map. Why not be the first to post a comment about A Cool Directory Map.

Post your comment

Subject:
Message:
0/1000 characters