473,886 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetFi leSystemEntries (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 directoryEntrie s
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 3721
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.G etDirectories(D irName)
GetDir(director y.name)
Next
End Sub
"Elmo Watson" <sp**********@n ospamYahoo.com> wrote in message
news:OX******** ******@TK2MSFTN GP12.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.GetFi leSystemEntries (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 directoryEntrie s
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******** ******@TK2MSFTN GP10.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.G etDirectories(D irName)
GetDir(director y.name)
Next
End Sub
"Elmo Watson" <sp**********@n ospamYahoo.com> wrote in message
news:OX******** ******@TK2MSFTN GP12.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.GetFi leSystemEntries (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 directoryEntrie s
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/frlrfSystemIODi rectoryClassGet FilesTo
pic.htm

From the VS.Net 2002 MSDN install. Should be able to find it at MSDN as
well.
' For Directory.GetFi les and Directory.GetDi rectories
' For File.Exists, Directory.Exist s

Imports System
Imports System.IO
Imports System.Collecti ons

' 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 RecursiveFilePr ocessor

'Entry point which delegates to C-style main function
Public Overloads Shared Sub Main()
Main(System.Env ironment.GetCom mandLineArgs())
End Sub

Overloads Public Shared Sub Main(args() As String)
Dim path As String
For Each path In args
If File.Exists(pat h) Then
' This path is a file
ProcessFile(pat h)
Else
If Directory.Exist s(path) Then
' This path is a directory
ProcessDirector y(path)
Else
Console.WriteLi ne("{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 ProcessDirector y(targetDirecto ry As String)
Dim fileEntries As String() = Directory.GetFi les(targetDirec tory)
' Process the list of files found in the directory
Dim fileName As String
For Each fileName In fileEntries
ProcessFile(fil eName)

Next fileName
Dim subdirectoryEnt ries As String() =
Directory.GetDi rectories(targe tDirectory)
' Recurse into subdirectories of this directory
Dim subdirectory As String
For Each subdirectory In subdirectoryEnt ries
ProcessDirector y(subdirectory)
Next subdirectory

End Sub 'ProcessDirecto ry

' Real logic for processing found files would go here.
Public Shared Sub ProcessFile(pat h As String)
Console.WriteLi ne("Processed file '{0}'.", path)
End Sub 'ProcessFile
End Class 'RecursiveFileP rocessor
"Elmo Watson" <sp**********@n ospamYahoo.com> wrote in message
news:OQ******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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.G etDirectories(D irName)
GetDir(director y.name)
Next
End Sub
"Elmo Watson" <sp**********@n ospamYahoo.com> wrote in message
news:OX******** ******@TK2MSFTN GP12.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.GetFi leSystemEntries (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 directoryEntrie s
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(str ing rootPath)
{
// Open the list
textBox2.Text += "<ul>";

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

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

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

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

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

// Close the list
textBox2.Text += "</ul>";
}
Hope this helps,

Mun

"Elmo Watson" <sp**********@n ospamYahoo.com> wrote in message
news:OX******** ******@TK2MSFTN GP12.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
1681
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 to create an XML structure that represents the contents of a directory structure. I call it a DirObj. Each DirObj needs a list node that can itself contaibn several more DirObj - effectively a 'recursive' definition. It looks something like...
7
567
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
2847
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 contains US leg and Europe leg (and others) US leg contains State tours (and others)
1
1172
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 help!! I want the vaild drives to be the top layer with 'children' drilling down through the directory structure.
9
8340
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) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA. Subfolder 2 contains FileB, FileC, FileD.
3
5680
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 progstatus. Does anyone know how to set this up with the code below? appreciate it. thank you. Private Function Dir(ByVal sDir As String)
5
2922
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 but when it tries to zip one of the directories it fails with the following error: "IOError: Permission denied: 'c:\\aaa\\temp'" The script I am using is:
2
2409
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 all the files and folders so that it would map the file system hierarchy.
3
2388
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 have the following OU hierarchy in my Active Direcoty: 1. NewYork 1.1 HR_Department 1.1.1 Computers 1.1.2 Users 1.2 SALES_Department 1.1.1 Computers
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10873
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10430
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7141
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.