By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

Recursive Directory Structure - System.IO

P: n/a

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?
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Elmo Watson" <sp**********@nospamYahoo.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...

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?

Nov 18 '05 #2

P: n/a
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?)
"Morgan" <mf****@spamcop.net> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
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
"Elmo Watson" <sp**********@nospamYahoo.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...

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?


Nov 18 '05 #3

P: n/a
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
"Elmo Watson" <sp**********@nospamYahoo.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
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?)
"Morgan" <mf****@spamcop.net> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
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
"Elmo Watson" <sp**********@nospamYahoo.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...

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?



Nov 18 '05 #4

P: n/a
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" <sp**********@nospamYahoo.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...

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>
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.