473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Recursive Directory Structure - System.IO


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
4 3676
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Foord | last post by:
I've been using the excellent XMLObject and have unfortunately come up against what *looks* to me like a bug - although it's very possible that the problem is mine !! I want to use XML object...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
25
by: Mike MacSween | last post by:
Regular viewers may want to turn off now. This will be an orchestral management system. Musicians and other staff being booked/paid for jobs. A job may contain other jobs, e.g: World Tour...
1
by: Dane Carty | last post by:
Hi, Using a TreeView component to create a tree of the directories within my file system. I can't get my head around the logic of the recursion. Anyone with a bigger brain is welcome to...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
3
by: Gabe Matteson | last post by:
I am trying to set the maximum value of the progress bar so that when a user searches through the specified directory they can see their status. the progress bar name is on form2 and is named...
5
by: Jandre | last post by:
Hi I am a python novice and I am trying to write a python script (most of the code is borrowed) to Zip a directory containing some other directories and files. The script zips all the files fine...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing...
3
by: fabiomoggi | last post by:
Hello Guys, I am developing a web application to manage Active Directory resources, and one of my tasks is to map Organizational Units hierarchy into a SQL Server database. Let's suppose that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.