You are here: Home > Articles > Article Display

Generating an XML file of your website's folders/files

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 can then use that file for example, in a content management system or a TreeView control.

Published: May 24, 2002
Tested with: ASP 3.0
Category: ASP
19,411 views

Introduction

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 can then use that file for example, in a content management system or a TreeView control.

The variables

First we declare our variables:

1 <%@Language="VBScript"%>
2 <%Option Explicit%>
3 <%Response.Buffer = True%>
4 <%
5 '-- set a 600 second timeout value in case it hangs
6 Server.ScriptTimeOut = 600
7 On Error Resume Next
8
9 Dim strRootFolder
10 Dim intLenRootFolder
11 Dim objFSO
12 Dim strXmlFile
13 Dim strVbCrLf
14 Dim strVbTab
15 Dim numTree
16 Dim objFile
17 ...

Then we set them equal to some values:

1 ...
2 strVbCrLf = VbCrLf
3 strVbTab = VbTab
4 numTree = 0
5 strRootFolder = Request.ServerVariables("APPL_PHYSICAL_PATH")
6 intLenRootFolder = Len(strRootFolder)
7 strXmlFile = "<root type=""root"" value=""Site Root"" url=""/"">" & strVbCrLf
8 ...

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


1 ...
2 '-- create an instance of the FSO object
3 Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
4 '-- open file for writing, create if not there, and open as text
5 Set objFile = objFSO.CreateTextFile(strRootFolder & "menuitems.xml", True, False)
6 '-- write root node
7 objFile.Write(strXmlFile)
8 ...

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


1 ...
2 Call TraverseSite(strRootFolder,numTree)
3
4 Sub TraverseSite(strFolder,thisTree)
5 ...

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:

  1. strRootFolder : the specific folder we are in
  2. 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:

1 ...
2      '-- reset the string which gets written to the XML file
3     strXmlFile = ""
4     '-- declare our variables in the sub
5     Dim objFolder, objSubFolder, objSubFile, i
6     Dim objThisFolder, objthisFile, strURL, strFolderURL
7     
8     '-- grab the folder passed in the sub
9     Set objFolder = objFSO.GetFolder(strFolder)
10     '-- grab its subfolders
11     Set objSubFolder = objFolder.SubFolders
12     '-- grab its files
13     Set objSubFile = objFolder.Files
14     
15     '-- properly nest the nodes for a nice display
16     For i = 1 To thisTree
17         strXmlFile = strXmlFile & strVbTab
18     Next
19     '-- get the folder path excluding the application root folder
20     strFolderURL = Mid(objFolder.Path, intLenRootFolder, Len(objFolder.Path))
21     '-- turn the physical path into a relative path
22     strFolderURL = Replace(strFolderURL, "\", "/")
23     '-- append node to the output unless it is the root folder
24     If thisTree > 0 Then
25         strXmlFile = strXmlFile & "<folder type=""folder"" value=""" & objFolder.Name & """ url=""" & strFolderURL & "/"">" & strVbCrLf
26         objFile.Write(strXmlFile)
27     End If
28     '-- reset the string output again
29     strXmlFile = ""
30     '-- if there are subfolders under it then run sub again for each one
31     If Not IsEmpty(objSubFolder) Then
32         For Each objThisFolder in objSubFolder
33             Call TraverseSite(objThisFolder.Path,thisTree + 1)
34         Next
35     End If
36 ...

Then we loop through the file contents of each folder we are in:

1 ...
2     '-- if there are files in the folder
3     If Not IsEmpty(objSubFile) Then
4         For Each objthisFile in objSubFile
5             '-- properly nest the nodes for a nice display
6             For i = 0 To thisTree + 1
7                 strXmlFile = strXmlFile & strVbTab
8             Next
9             '-- get the virtual path
10             strURL = Mid(objthisFile.Path, intLenRootFolder, Len(objthisFile.Path))
11             strURL = Replace(strURL, "\", "/")
12             '-- create output string
13             strXmlFile = strXmlFile & "<document type=""document"" value=""" & objthisFile.Name & """ url=""" & strURL & """/>" & strVbCrLf
14         Next
15         '-- write output to XML file
16         objFile.Write(strXmlFile)
17         strXmlFile = ""
18     End If
19     For i = 1 To thisTree
20         strXmlFile = strXmlFile & strVbTab
21     Next
22     '-- close the folder node
23     If thisTree > 0 Then
24         strXmlFile = strXmlFile & "</folder>" & strVbCrLf
25     End If
26     objFile.Write(strXmlFile)
27     strXmlFile = ""
28     '-- show the progress of the code as it goes through folders
29     Response.Write("Folder <font color=""#FF0000"">" & strFolderURL & "</font> written!<br>")
30     Response.Flush
31 End Sub
32
33 '--write end root node
34 objFile.Write("</root>")
35 objFile.close
36 Set objFile = Nothing
37 Set objFSO = Nothing
38 %>

XML file created

The resulting XML file, menuitems.xml, should look something like this:

Example of XML file produced

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:

1 ...
2 strXmlFile = strXmlFile & "<document type=""document"" value=""" & objthisFile.Name & """ url=""" & strURL & """" & " datelastmodified=""" & objthisFile.DateLastModified & """/>" & strVbCrLf
3 ...

And the expanded XML file output would then look like this:

XML file output expanded to include DateLastModified

Conclusion

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.

 



Other articles in this category
  1. Exporting Word files to HTML
    March 5, 2003
    In this article we will first discuss the case for and against using Word as your HTML editor. Then we will see how to properly save a Word file to smaller, more compact HTML files. Third and last, we will see how to do this through code, and possibly create a batch process for converting numerous Word files to HTML at once.
  2. GetRows VBScript Class - Part III: Paging the results
    January 16, 2003
    In Part I of this series, we saw how to create a VBScript class to query our database using the very fast GetRows() method, and return a recordset as a local array. In Part II, we extended the class to allow ADDing and UPDATEing a row in the database. In this Part III, we will expand the class further to allow pagination of the returned recordset.
  3. Dynamic Tree Menu of your site
    May 31, 2002
    We'll see how to create a menu system that is cross-browser and includes all your site's folders/files. It uses ASP, XML and DHTML and by simply copying it to your site you have an instant Windows Explorer-like navigation of the contents.
  4. Downloading any file using ASP, FSO and the ADODB Stream object
    May 8, 2002
    In this article, we will see how to allow a user to download any file from our web server. They will see a prompt, giving them the option of opening or saving it, rather than simply opening it which is the default. We can achieve this using the FSO and ADODB objects.
  5. Calling MS Access Parameterized Queries from ASP
    April 30, 2002
    Instead of passing a SQL query through your ASP code against Microsoft Access as you would normally do, you can use the Queries design interface to create them in Access and then call them from your ASP code. It makes things easier to edit and maintain, and the results are returned faster.