Recursive Directory Structure - System.IO

E

Elmo Watson

Is there a way, with the System.IO class, to do a recursive list of a
directory structure?

For instance, in DirectoryInfo, you have GetDirectories and GetFiles ....

In Directory, you have Directory.GetFileSystemEntries(path), but I would
like to know how to put this together, knowing which entry is a Subdirectory
and which entry is a file, and make a recursive list of the Directory
structure below a specific path - - -

For instance, I have:
Dim str As String
For Each str In directoryEntries
response.write (str & "<br>")
' here, I'd like to figure out whether it's a subdirectory or a file - -
if it's a subdirectory, go into it and list it's files and
subdirectories - - - - total recursiveness is what I'm after and it's got me
loony.
Next Str

Any ideas?
 
M

Morgan

There is a sample in the MSDN help that does -exactly- what you are looking
to do.
Essentially you have to create a recursive procedure and iterate through the
directories.
<Psuedo code>
Private Sub GetDir(DirName as string)
For each directory in directoryInfo.GetDirectories(DirName)
GetDir(directory.name)
Next
End Sub
 
E

Elmo Watson

I found what I have in MSDN - - I have not been able, even after your
message, of finding the example - - - are you saying MSDN Online? (got a
link?)
 
M

Morgan

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemIODirectoryClassGetFilesTo
pic.htm

From the VS.Net 2002 MSDN install. Should be able to find it at MSDN as
well.


' For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists

Imports System
Imports System.IO
Imports System.Collections



' Takes an array of file names or directory names on the command line.
' Determines what kind of name it is and processes it appropriately
Public Class RecursiveFileProcessor

'Entry point which delegates to C-style main function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub

Overloads Public Shared Sub Main(args() As String)
Dim path As String
For Each path In args
If File.Exists(path) Then
' This path is a file
ProcessFile(path)
Else
If Directory.Exists(path) Then
' This path is a directory
ProcessDirectory(path)
Else
Console.WriteLine("{0} is not a valid file or
directory.", path)
End If
End If
Next path
End Sub 'Main


' Process all files in the directory passed in, and recurse on any
directories
' that are found to process the files they contain
Public Shared Sub ProcessDirectory(targetDirectory As String)
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
' Process the list of files found in the directory
Dim fileName As String
For Each fileName In fileEntries
ProcessFile(fileName)

Next fileName
Dim subdirectoryEntries As String() =
Directory.GetDirectories(targetDirectory)
' Recurse into subdirectories of this directory
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory

End Sub 'ProcessDirectory

' Real logic for processing found files would go here.
Public Shared Sub ProcessFile(path As String)
Console.WriteLine("Processed file '{0}'.", path)
End Sub 'ProcessFile
End Class 'RecursiveFileProcessor
 
M

Munsifali Rashid

Here's something I wrote a short while ago, in C#.


private void walkFolders(string rootPath)
{
// Open the list
textBox2.Text += "<ul>";

// Iterate through each folder
foreach (string folder in System.IO.Directory.GetDirectories(rootPath))
{
// Remove the path
string b = "<li>" + folder.Replace(rootPath + "\\", string.Empty);

// Put the folder name in the textbox
textBox2.Text += b + Environment.NewLine;

// Walk through this folder
walkFolders(folder);
}

// Iterate through the files in this folder
foreach (string file in System.IO.Directory.GetFiles(rootPath))
{
// Remove path and prefix with LI
string b = "<li>" + file.Replace(rootPath + "\\", string.Empty);

// Put the filename in the textbox
textBox2.Text += b + Environment.NewLine;
}

// Close the list
textBox2.Text += "</ul>";
}


Hope this helps,

Mun



Elmo Watson said:
Is there a way, with the System.IO class, to do a recursive list of a
directory structure?

For instance, in DirectoryInfo, you have GetDirectories and GetFiles ....

<Snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top