C:\Base\Level1\ level2\level3\L evel4\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 IncludeSubFolde rs As
Boolean = True) As XmlNode
Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(D irPath)
Dim objSubFolder As DirectoryInfo
'add a node for each file
For Each objFileInfo In objDir.GetFiles ()
Dim MyFileNode As XmlElement = XmlDoc.CreateEl ement("Node")
MyFileNode.SetA ttribute("type" , "file")
MyFileNode.SetA ttribute("id", intCount.ToStri ng)
intCount += 1
MyFileNode.SetA ttribute("type" , objFileInfo.Ext ension)
MyFileNode.SetA ttribute("size" , objFileInfo.Len gth.ToString)
MyFileNode.SetA ttribute("text" , objFileInfo.Nam e)
ParentNode.Appe ndChild(MyFileN ode)
Next
'call recursively to do sub folders
'if you don't want this set optional
'parameter to false
'add the folder node
If IncludeSubFolde rs Then
For Each objSubFolder In objDir.GetDirec tories()
Dim DirNode As XmlElement = XmlDoc.CreateEl ement("Node")
DirNode.SetAttr ibute("type", "folder")
DirNode.SetAttr ibute("id", intCount.ToStri ng)
intCount += 1
DirNode.SetAttr ibute("text", objSubFolder.Na me)
ParentNode.Appe ndChild(DirNode )
GetFiles(XmlDoc , DirNode, objSubFolder.Fu llName, True)
Next
End If
Return ParentNode
End Function