Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Display a directory tree

Total Hit ( 3952)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Thanks to the GetDirectories and GetFiles methods of the System.IO.Directory class, you need very little code to iterate over all the directories and files of a directory tree. For example, the following code snippet prints the structure of a directory tree and (optionally) the name of files in each directory:

Click here to copy the following block
' Assumes the following imports:
' Imports System.IO

Sub PrintDirTree(ByVal dir As String, ByVal showFiles As Boolean, _
  Optional ByVal level As Integer = 0)
  Dim subdir As String
  Dim fname As String

  ' Display the name of this directory with correct indentation.
  Console.WriteLine(New String("-"c, level * 2) & dir)

  Try
    ' Display all files in this directory, with correct indentation.
    If showFiles Then
      For Each fname In Directory.GetFiles(dir)
        Console.WriteLine(New String(" "c, level * 2 + 2) & fname)
      Next
    End If
    ' A recursive call for all the subdirectories in this directory.
    For Each subdir In Directory.GetDirectories(dir)
      PrintDirTree(subdir, showFiles, level + 1)
    Next
  Catch
    ' Do nothing if any error (presumably "Drive not ready").
  End Try
End Sub

'You can pass a directory name to the PrintDirTree procedure, or print the directory tree of all the drives in your system, by using this code:
Dim rootDir As String
For Each rootDir In Directory.GetLogicalDrives
  PrintDirTree(rootDir, True)
Next


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.