Click here to Skip to main content
14,601,926 members

Recursive function to read a directory structure

Rate this:
4.14 (13 votes)
Please Sign up or sign in to vote.
4.14 (13 votes)
11 Sep 2005
How to use a recursive function to read a directory structure and save it to XML.

Image 1


In this article, I will show you how to use a recursive function to read a directory structure and then save it to XML, but first of all let me tell you the purpose of this application. I had to develop a desktop application in Flash, and it had to open audio files from the PC. I know that Flash can't execute OpenFileDialogBox and get the file path, ...but it can read XML files! So, I decided to create a default directory ("Media Library") where the user could organize the audio files in the way he wants.

But, how will the Flash know what the directory has??? From Flash I call my dirSinc.exe (.NET app.) that reads the complete "Media Library" directory structure and saves it to dirSinc.XML. In the Flash application there is a FlashTreeView (FTree component) that shows in a TreeView the content of "Media Library" and from where the user can select and open a file.

I think this introduction is enough to understand what we are gonna let's go to the code!!

Using the code

I decided to do a generic example application for this article, so the form has a FolderBrowserDialog to select the folder you want to work with. When you try it, don't select the "C:\windows" directory...because it will generate an immense XML!! ;)

Image 2

First of all, we have to declare the object that will manipulate the XML document. It has to be class level because it will be used by more than one function:

Public Class frmXML Inherits System.Windows.Forms.Form
    Dim xmlDoc As New XmlDocument 'Xml Document

btnSinc_Click event

Considering you already know how to create an XML document with its elements and attributes, let's focus on the createRootNode() call.

Private Sub btnSinc_Click(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles 
    If txtPath.Text = "" Then
        Exit Sub
    End If
        'Clears the xmlDoc for multiple process, 
        'because xmlDoc is a class level object
        lblStatus.Text = "Readind directory structure..."
        'First Chid: Header
                                          "xml", "version='1.0'"))
        xmlDoc.Save("structure_of_" & _
          txtPath.Text.Substring(txtPath.Text.LastIndexOf("\") + 1) & _
        lblStatus.Text = "The XML document was created." 
    Catch ex As Exception 
    End Try
End Sub


The XML format I adopted for this example has a "myRoot" node with its label and description. (This is what this Sub creates.) From here we will create the "folder" and "files" nodes with their label, description, filePath, and size attributes. Since "myRoot" node was appended, we call the loopNodes() function sending the oElmntRoot: "myRoot" node (user selected folder), txtPath.Text: the full path to the user selected folder.


Finally, we are at the recursive function. Here we have two 'For Each' loops, one for folders and the other for files. We first start with the folders loop that will call itself as many times as the number of folders in order to find out if a folder has subfolders (child nodes) inside.

ofs.GetDirectories will let us loop through all the subfolders in the current folder, if true we create the XML element with its attributes, and then make the recursive call until there are no subfolders in the current folder. There begins the 'files' loop creating the corresponding XML element. But this 'file' loop doesn't make a recursive call because a file does not have subfiles. :)

Private Function loopNodes(ByVal oElmntParent As XmlElement, _
                                       ByVal strPath As String)
    Dim d As DirectoryInfo
    Dim ofs As New DirectoryInfo(strPath & "\")
    'Folders Loop -------------------------------------------------
    'Gets the directories of the current node. 
    'It changes for each recursive call
    For Each d In ofs.GetDirectories  
        'Manipulates the sub folders nodes
        Dim oElmntChild As XmlElement  
        oElmntChild = xmlDoc.CreateElement("folder") 
        oElmntChild.SetAttribute("label", d.Name) 
        'Adds the "folder" node
        'Recursive call
        loopNodes(oElmntChild, strPath & "\" & d.Name) 
    'Files Loop ------------------------------------------------------ 
    Dim oFile As FileInfo
    For Each oFile In ofs.GetFiles
        Dim oElmntLeaf As XmlElement 'Manipulates the files nodes
        oElmntLeaf = xmlDoc.CreateElement("file")
        oElmntLeaf.SetAttribute("label", oFile.Name)
        oElmntLeaf.SetAttribute("filePath", strPath)
        oElmntLeaf.SetAttribute("bytesSize", CType(oFile.Length, String))
        oElmntParent.AppendChild(oElmntLeaf) 'Adds the "file" node
End Function

The key point to understand in the recursive function is that when it calls itself it creates new instances of the objects inside, and when it finishes, returns to the point from where it was called. May be the first time you'll need to debug the function step by step to understand what is happening in each loop.

Tip: This function could be used to fill a TreeView, provided the structure is not very big. In that case it maybe better to fill the folders while you open each one.

Hope this article helps you!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Damian Castroviejo
Web Developer
United States United States
I started working as Multimedia/Web designer in Buenos Aires in 1998, and later on I felt attraction for coding.
I specialized in Vb.Net languaje, but I also like to code in C#, ActionScript and clasic ASP.
I'm currently working in Miami area.

Comments and Discussions

GeneralMy vote of 5 Pin
Member 102459371-Sep-13 10:28
MemberMember 102459371-Sep-13 10:28 
GeneralMy vote of 5 Pin
satishk4u12-Jan-11 20:47
Membersatishk4u12-Jan-11 20:47 
GeneralThanks Pin
karenpayne18-Nov-10 11:54
Memberkarenpayne18-Nov-10 11:54 
GeneralMy vote of 5 Pin
myaccountram14-Aug-10 9:51
Membermyaccountram14-Aug-10 9:51 
GeneralAwesome Post Pin
Vijay Mesa19-Oct-09 4:09
MemberVijay Mesa19-Oct-09 4:09 
QuestionI need this too but in C++ Pin
Carl Owen28-Jul-08 11:36
MemberCarl Owen28-Jul-08 11:36 
AnswerRe: I need this too but in C++ Pin
VSCurtis9-Mar-20 21:16
MemberVSCurtis9-Mar-20 21:16 
GeneralRequest Pin
eyemkent9-May-08 21:29
Membereyemkent9-May-08 21:29 
GeneralRe: Request Pin
Damian Castroviejo10-May-08 7:05
MemberDamian Castroviejo10-May-08 7:05 
GeneralThanks Pin
jometrv4-Mar-08 23:07
Memberjometrv4-Mar-08 23:07 
GeneralRe: Thanks Pin
Damian Castroviejo6-Mar-08 9:57
MemberDamian Castroviejo6-Mar-08 9:57 
Questionbroken links & missing code Pin
rcbrules22-Oct-07 13:18
Memberrcbrules22-Oct-07 13:18 
AnswerRe: broken links & missing code Pin
Damian Castroviejo24-Oct-07 17:11
MemberDamian Castroviejo24-Oct-07 17:11 
GeneralRecursive function to create a directory structure Pin
Sumangal Singh8-Nov-06 19:10
MemberSumangal Singh8-Nov-06 19:10 
GeneralRe: Recursive function to create a directory structure Pin
Damian Castroviejo9-Nov-06 15:16
MemberDamian Castroviejo9-Nov-06 15:16 
GeneralVery good article Pin
Austin Amuok6-Jan-06 18:47
MemberAustin Amuok6-Jan-06 18:47 
GeneralThis is Exactly what I needed! Pin
mizzl232mike14-Nov-05 10:59
Membermizzl232mike14-Nov-05 10:59 
Big Grin | :-D :-DThankyou! This is great code. Just what I needed. Not to complicated and a very useful application for flash. I was making a Picture slideshow with flash and needed a way to create an xml file that would list hundreds of photos and pictures instead of keying them in manually. You saved me hours of hard work! Thanks a lot!!! Big Grin | :-D

GeneralRe: This is Exactly what I needed! Pin
Damian Castroviejo16-Nov-05 14:28
MemberDamian Castroviejo16-Nov-05 14:28 
GeneralRe: This is Exactly what I needed! Pin
mizzl232mike16-Nov-05 20:19
Membermizzl232mike16-Nov-05 20:19 
GeneralRe: This is Exactly what I needed! Pin
Damian Castroviejo17-Nov-05 17:37
MemberDamian Castroviejo17-Nov-05 17:37 
GeneralSome suggestions Pin
Pete Appleton13-Sep-05 22:11
MemberPete Appleton13-Sep-05 22:11 
GeneralAnother similar work Pin
Alberto Venditti13-Sep-05 12:43
MemberAlberto Venditti13-Sep-05 12:43 
Generaldon't use this in production-code Pin
KristofVerbiest12-Sep-05 0:44
MemberKristofVerbiest12-Sep-05 0:44 
GeneralRe: don't use this in production-code Pin
Damian Castroviejo12-Sep-05 3:31
MemberDamian Castroviejo12-Sep-05 3:31 
GeneralRe: don't use this in production-code Pin
Kristof Verbiest31-May-06 1:04
MemberKristof Verbiest31-May-06 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 11 Sep 2005


74 bookmarked