Friday, 2 January 2015

Parse FTP XML Directory add to collection



I retrieve a remote FTP structure in XML format. I'm trying to parse the XML and add each item to a collection like,


TestDir/kernel.dll TestDir/New folder/ TestDir/New folder/mycert.avi TestDir/New folder/asdasd TestDir/New folder/asdasd/asdasd TestDir/New folder/asdasd/asdasd/asdasdasdasd TestDir/New folder/asdasd/asdasd/asdasdasdasd/done TestDir/I7Folder/ TestDir/I7Folder/sdfsdfg.txt TestDir/I7Folder/sdfsdfsdf.txt


Here is my XML data,



<?xml version="1.0" encoding="utf-8" ?>
<dirTree>
<dir name="TestDir">
<file sz="364" dt="Tue, 30 Dec 2014 22:56:00 -0500">kernel.dll</file>
<dir name="New folder">
<file sz="309" dt="Tue, 30 Dec 2014 23:00:00 -0500">mycert.avi</file>
<dir name="asdasd">
<dir name="asdasd" />
<dir name="asdasdasdasd">
<dir name="done" />
</dir>
</dir>
</dir>
</dir>
<dir name="I7Folder">
<file sz="289" dt="Tue, 30 Dec 2014 22:57:00 -0500">sdfsdfg.txt</file>
<file sz="280" dt="Tue, 30 Dec 2014 22:57:00 -0500">sdfsdfsdf.txt</file>
</dir>
<dir name="data">
<file sz="4727" dt="Tue, 30 Dec 2014 22:56:00 -0500">dasdasd.txt</file>
<file sz="78419" dt="Tue, 30 Dec 2014 22:56:00 -0500">asdasdas.txt</file>
<dir name="New folder">
<dir name="New folder" />
<dir name="New folder (2)">
<dir name="New folder" />
</dir>
</dir>
<dir name="images" />
</dir>
</dir>
</dirTree>


My code below produces the incorrect output where it would produce something like,


TestDir/New folder/New folder/asdasd/asdasd/asdasdasdasd/done/I7Folder/sdfsdfsdf.txt


How can I perform the loop so that it doesn't continue using folder names that don't pertain to the correct location. I think it's a simple problem but I can't seem to wrap my head around it as I've been looking at it for to long.


Private Sub LoopDirectory(x As XmlNode, strDir As String, colCurrentFiles As Collection, colCurrentFolders As Collection, strParentDirectory As String) Debug.Write(strDir & vbNewLine) Dim y As XmlNode



For Each y In x.ChildNodes


Select Case y.Name
Case "file"
colCurrentFiles.Add(strDir & "\" & y.InnerText)
Case "dir"

If strDir = "" Then
strDir = strParentDirectory & "\" & y.Attributes(0).Value
Else
strDir = strDir & "\" & y.Attributes(0).Value
End If



Debug.Write(strDir & vbNewLine)

colCurrentFolders.Add(strDir)

LoopDirectory(y, strDir, colCurrentFiles, colCurrentFolders, strParentDirectory)


End Select
Next


End Sub


No comments:

Post a Comment