Using the File System Object (FSO) we can traverse through our website’s contents and write them out in a nicely nested form in an XML file. We could then use that file in a content management system or a TreeView control for example.
The variables
First we declare our variables:
Then we set them equal to some values:
The strXmlFile is the root node of our XML file and we can set it equal to whatever we want as long as the type is equal to root.
Creating and writing to the XML file
You can change the location of this file by adding folders in the Windows format. For example, to put it in a folder called navigation, change the location like so:
Set objFile = objFSO.CreateTextFile(strRootFolder & “navigation\menuitems.xml“, True, False)
Traversing through the site
We create a sub called TraverseSite, which is responsible for going through the folders of the site and creating nodes in our XML file. It accepts two parameters:
- strRootFolder : the specific folder we are in
- numTree: how many levels deep we are in the website folder structure
When we first call it, we pass the root folder to it, and 0 for the folder level. This sub gets called within itself as it goes deeper through the folders and the parameters change accordingly.
The basic logic for this sub is:
Take a folder Show name of folder without closing xml tag If there are subfolders Then For each subfolder Run same procedure with new folder and level Next End If If there are subfiles Then Show subfiles with closing xml tag End If Add closing xml tag
So, first we traverse through the folders:
Then we loop through the file contents of each folder we are in:
XML file created
The resulting XML file, menuitems.xml, should look something like this:
Including other properties in the XML file
The name and path of a folder/file may be the basic output here, but we can easily expand them to include other properties as well. For example, using FSO we can also output the size, date created, date last modified, etc. To add for example the date last modified of files to the XML file output, change the strXmlFile output above (in the file output section) like so:
And the expanded XML file output would then look like this:
We can of course go beyond the FSO object for outputing properties in the XML file. We could for example, instantiate a component that reads image properties if the file we are on happens to be an image, and include those properties as well in the output – like image width, height etc.
2 Responses to Generating an XML file of your website’s folders/files