C:\Base\Level1\level2\level3\Level4\Filename.TXT
C:\Base is the base (root) dir.
I need to parse the folder name Level1 thru level4 and assign a
corresponding folder level ID 2,3,4) to these folders.
Any idea how to do this?
Thanks in advance.
Do something like this (one I prepared earlier... you don't need to use XML.
To add the folder level counter add a line to increment a class-level counter
inside the recursion loop and include its value in the output:
Private Function GetFiles(ByRef XmlDoc As XmlDocument, ByRef ParentNode
As XmlElement, ByVal DirPath As String, Optional ByVal IncludeSubFolders As
Boolean = True) As XmlNode
Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo
'add a node for each file
For Each objFileInfo In objDir.GetFiles()
Dim MyFileNode As XmlElement = XmlDoc.CreateElement("Node")
MyFileNode.SetAttribute("type", "file")
MyFileNode.SetAttribute("id", intCount.ToString)
intCount += 1
MyFileNode.SetAttribute("type", objFileInfo.Extension)
MyFileNode.SetAttribute("size", objFileInfo.Length.ToString)
MyFileNode.SetAttribute("text", objFileInfo.Name)
ParentNode.AppendChild(MyFileNode)
Next
'call recursively to do sub folders
'if you don't want this set optional
'parameter to false
'add the folder node
If IncludeSubFolders Then
For Each objSubFolder In objDir.GetDirectories()
Dim DirNode As XmlElement = XmlDoc.CreateElement("Node")
DirNode.SetAttribute("type", "folder")
DirNode.SetAttribute("id", intCount.ToString)
intCount += 1
DirNode.SetAttribute("text", objSubFolder.Name)
ParentNode.AppendChild(DirNode)
GetFiles(XmlDoc, DirNode, objSubFolder.FullName, True)
Next
End If
Return ParentNode
End Function