Skip to content

Instantly share code, notes, and snippets.

@evagoras
Created September 28, 2023 13:55
Show Gist options
  • Save evagoras/337ce1029ca8aaaf8711a40ce672f63a to your computer and use it in GitHub Desktop.
Save evagoras/337ce1029ca8aaaf8711a40ce672f63a to your computer and use it in GitHub Desktop.
Generating an XML file of your website’s folders/files
<%@Language="VBScript"%>
<%Option Explicit%>
<%Response.Buffer = True%>
<%
'-- set a 600 second timeout value in case it hangs
Server.ScriptTimeOut = 600
On Error Resume Next
Dim strRootFolder
Dim intLenRootFolder
Dim objFSO
Dim strXmlFile
Dim strVbCrLf
Dim strVbTab
Dim numTree
Dim objFile
...
...
'-- if there are files in the folder
If Not IsEmpty(objSubFile) Then
For Each objthisFile in objSubFile
'-- properly nest the nodes for a nice display
For i = 0 To thisTree + 1
strXmlFile = strXmlFile & strVbTab
Next
'-- get the virtual path
strURL = Mid(objthisFile.Path, intLenRootFolder, Len(objthisFile.Path))
strURL = Replace(strURL, "\", "/")
'-- create output string
strXmlFile = strXmlFile & "<document type=""document"" value=""" & objthisFile.Name
& """ url=""" & strURL & """/>" & strVbCrLf
Next
'-- write output to XML file
objFile.Write(strXmlFile)
strXmlFile = ""
End If
For i = 1 To thisTree
strXmlFile = strXmlFile & strVbTab
Next
'-- close the folder node
If thisTree > 0 Then
strXmlFile = strXmlFile & "</folder>" & strVbCrLf
End If
objFile.Write(strXmlFile)
strXmlFile = ""
'-- show the progress of the code as it goes through folders
Response.Write("Folder <font color=""#FF0000"">" & strFolderURL & "</font> written!<br>")
Response.Flush
End Sub
'--write end root node
objFile.Write("</root>")
objFile.close
Set objFile = Nothing
Set objFSO = Nothing
%>
...
strXmlFile = strXmlFile & "<document type=""document"" value=""" & objthisFile.Name
& """ url=""" & strURL & """" & " datelastmodified=""" & objthisFile.DateLastModified & """/>" & strVbCrLf
...
...
strVbCrLf = VbCrLf
strVbTab = VbTab
numTree = 0
strRootFolder = Request.ServerVariables("APPL_PHYSICAL_PATH")
intLenRootFolder = Len(strRootFolder)
strXmlFile = "<root type=""root"" value=""Site Root"" url=""/"">" & strVbCrLf
...
...
'-- reset the string which gets written to the XML file
strXmlFile = ""
'-- declare our variables in the sub
Dim objFolder, objSubFolder, objSubFile, i
Dim objThisFolder, objthisFile, strURL, strFolderURL
'-- grab the folder passed in the sub
Set objFolder = objFSO.GetFolder(strFolder)
'-- grab its subfolders
Set objSubFolder = objFolder.SubFolders
'-- grab its files
Set objSubFile = objFolder.Files
'-- properly nest the nodes for a nice display
For i = 1 To thisTree
strXmlFile = strXmlFile & strVbTab
Next
'-- get the folder path excluding the application root folder
strFolderURL = Mid(objFolder.Path, intLenRootFolder, Len(objFolder.Path))
'-- turn the physical path into a relative path
strFolderURL = Replace(strFolderURL, "\", "/")
'-- append node to the output unless it is the root folder
If thisTree > 0 Then
strXmlFile = strXmlFile & "<folder type=""folder"" value=""" & objFolder.Name & """
url=""" & strFolderURL & "/"">" & strVbCrLf
objFile.Write(strXmlFile)
End If
'-- reset the string output again
strXmlFile = ""
'-- if there are subfolders under it then run sub again for each one
If Not IsEmpty(objSubFolder) Then
For Each objThisFolder in objSubFolder
Call TraverseSite(objThisFolder.Path,thisTree + 1)
Next
End If
...
...
Call TraverseSite(strRootFolder,numTree)
Sub TraverseSite(strFolder,thisTree)
...
...
'-- create an instance of the FSO object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'-- open file for writing, create if not there, and open as text
Set objFile = objFSO.CreateTextFile(strRootFolder & "menuitems.xml", True, False)
'-- write root node
objFile.Write(strXmlFile)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment