1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
'========================================================================= ' GoogleSitemap.vbs ' VERSION: 1.0 ' AUTHOR: Brian Steinmeyer ' EMAIL: sigkill@sigkillit.com ' WEB: http://sigkillit.com ' DATE: 4/1/2012 ' COMMENTS: Traverse a local website root folder and generates a Google ' Sitemap .xml file based on the folder structure. Modify the variables ' in the settings section accordingly. The variables are explained below: ' conLocalRoot - Local Website Root Folder ' conSitemap - Google Sitemap XML file to output ' conDefaultChangeFreq - Default Change Frequency for Sitemap ' conDefaultPriority - Default Priority for Sitemap ' blnSortAtoZ - Option to sort the sitemap alphabetically ' arrValidFileExtension - list of Valid file extensions to include int he sitemap ' arrDefaultDocument - list of valid default document names which are not added to the sitemap ' arrExcludedFolder - List of folders to exclude from the sitemap ' arrExcludedFile - List of file names to exclude from the sitemap '========================================================================= Option Explicit ' ------ SCRIPT CONFIGURATION ------ Const conLocalRoot = "C:\inetpub\wwwroot\" Const conSitemap = "C:\inetpub\wwwroot\sitemap.xml" Const conWebsiteFQDN = "http://www.domain.com/" Const conDefaultChangefreq = "monthly" Const conDefaultPriority = "0.5" Const blnSortAtoZ = False Dim arrValidFileExtension: arrValidFileExtension = array("php","asp","aspx","htm","html","shtml") Dim arrDefaultDocument: arrDefaultDocument = array("index.php","index.asp","index.aspx","index.htm","index.html","index.shtml") Dim arrExcludedFolder: arrExcludedFolder = array("_includes","css","images") Dim arrExcludedFile: arrExcludedFile = array("404.php") ' ------ END CONFIGURATION ------ 'Validate Arrays Dim blnValidFileExtensionIsArray: blnValidFileExtensionIsArray = False Dim blnDefaultDocumentIsArray: blnDefaultDocumentIsArray = False Dim blnExcludedFolderIsArray: blnExcludedFolderIsArray = False Dim blnExcludedFileIsArray: blnExcludedFileIsArray = False If IsArray(arrValidFileExtension) Then blnValidFileExtensionIsArray = True End If If IsArray(arrDefaultDocument) Then blnDefaultDocumentIsArray = True End If If IsArray(arrExcludedFolder) Then blnExcludedFolderIsArray = True End If If IsArray(arrExcludedFile) Then blnExcludedFileIsArray = True End If 'Crawl Local Webroot Folder Dim arrResults() Call CrawlFolder(conLocalRoot, 0, arrResults) 'Optionally Sort Results Array - Maybe Move this If blnSortAtoZ = True Then Call BubbleSort(arrResults) End If 'Write Sitemap XML File WriteSiteMap(arrResults) 'End Script Wscript.Echo "Finished" Private Function CrawlFolder(FolderToCrawl, intFiles, ByRef arrFiles) 'Start Crawling Folder Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objFolder: Set objFolder = objFSO.GetFolder(FolderToCrawl) Dim blnFolderIsExcluded: blnFolderIsExcluded = False 'Check If We Should Exclude This Folder If blnExcludedFolderIsArray = True Then Dim strExcludedFolder For Each strExcludedFolder in arrExcludedFolder If StrComp(objFolder.Name,strExcludedFolder,1) = 0 Then blnFolderIsExcluded = True End If Next End If 'Parse Files in Folder If blnFolderIsExcluded = False Then Dim objFiles: Set objFiles = objFolder.Files Dim objFile, strResult Dim strDefaultDocument, strValidFileExtension, strExcludedFile Dim blnFileIsExcluded, blnFileExtensionIsValid, blnDefaultDocument For Each objFile In objFiles blnFileIsExcluded = False blnFileExtensionIsValid = False blnDefaultDocument = False strResult = "" 'Check If We Should Exclude This File If blnExcludedFileIsArray = True Then For Each strExcludedFile in arrExcludedFile If StrComp(objFile.Name,strExcludedFile,1) = 0 Then blnFileIsExcluded = True End If Next End If 'Check If We Should Exclude This File Extension If blnValidFileExtensionIsArray = True Then For Each strValidFileExtension in arrValidFileExtension If StrComp(objFSO.GetExtensionName(objFile.Name),strValidFileExtension,1) = 0 Then blnFileExtensionIsValid = True End If Next End If If blnFileIsExcluded = False AND blnFileExtensionIsValid = True Then strResult = objFile.Path ReDim Preserve arrFiles(intFiles) arrFiles(intFiles) = strResult intFiles = intFiles + 1 End If Next 'Crawl Subfolders Dim objSubFolder For Each objSubFolder In objFolder.SubFolders Call CrawlFolder(objSubFolder.Path, intFiles, arrResults) Next End If CrawlFolder = arrFiles Set objFSO = Nothing Set objFolder = Nothing Set objFiles = Nothing End Function Private Sub WriteSiteMap(arrSites) Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(conSitemap) Then Dim strPath, strUrl, objNodeLoc, objNodeLastmod Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM") xmlDoc.preserveWhiteSpace = False xmlDoc.Async = False Dim arrAppend() Dim intAppend: intAppend = 0 If xmlDoc.Load(conSitemap) Then For Each strPath in arrSites strUrl = ConvertLocalPathToUrl(strPath) Set objNodeLoc = xmlDoc.selectSingleNode("/urlset/url " & "[loc = '" & strUrl & "']") If Not objNodeLoc Is Nothing then 'Found a Match Update the Date Set objNodeLastmod = xmlDoc.selectSingleNode("/urlset/url " & "[loc = '" & strURL & "']/lastmod") If Not objNodeLastmod Is Nothing Then objNodeLastmod.Text = GetDate(objFSO.GetFile(strPath).DateLastModified) End If xmlDoc.Save conSitemap Else 'Append URL to sitemap.xml Dim objRoot: Set objRoot = xmlDoc.documentElement Dim objRecord: Set objRecord = xmlDoc.createElement("url") 'objRecord.setAttribute "xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") objRoot.appendChild objRecord Dim objFieldValue 'Location Set objFieldValue = xmlDoc.createElement("loc") objFieldValue.Text = strUrl objRecord.appendChild objFieldValue 'Last Modified Set objFieldValue = xmlDoc.createElement("lastmod") objFieldValue.Text = GetDate(objFSO.GetFile(strPath).DateLastModified) objRecord.appendChild objFieldValue 'Change Frequency Set objFieldValue = xmlDoc.createElement("lastmod") objFieldValue.Text = conDefaultChangefreq objRecord.appendChild objFieldValue 'Priority Set objFieldValue = xmlDoc.createElement("lastmod") objFieldValue.Text = conDefaultPriority objRecord.appendChild objFieldValue 'Save Sitemap.xml xmlDoc.Save conSitemap End If Next Else Wscript.Echo "Error Loading sitemap.xml Or sitemap.xml is NOT valid XML" End If 'Cheap Hack to Cleanup Appends Set xmlDoc = Nothing Dim objTextFile: Set objTextFile = objFSO.OpenTextFile(conSitemap, 1) Dim strText: strText = objTextFile.ReadAll objTextFile.Close strText = Replace(strText," xmlns=" & chr(34) & chr(34),"") Set objTextFile = objFSO.OpenTextFile(conSitemap, 2) objTextFile.Write strText objTextFile.Close Else 'Make New Sitemap File Call Logger(conSitemap,"<?xml version=" & chr(34) & "1.0" & chr(34) & " encoding=" & chr(34) & "UTF-8" & chr(34) & "?>",True) Call Logger(conSitemap,"<urlset xmlns=" & chr(34) & "http://www.sitemaps.org/schemas/sitemap/0.9" & chr(34) & ">",False) Dim strLoc For Each strLoc in arrSites Call Logger(conSitemap,vbTab & "<url>",False) Call Logger(conSitemap,vbTab & vbTab & "<loc>" & ConvertLocalPathToUrl(strLoc) & "</loc>",False) Call Logger(conSitemap,vbTab & vbTab & "<lastmod>" & GetDate(objFSO.GetFile(strLoc).DateLastModified) & "</lastmod>",False) Call Logger(conSitemap,vbTab & vbTab & "<changefreq>" & conDefaultChangefreq & "</changefreq>",False) Call Logger(conSitemap,vbTab & vbTab & "<priority>" & conDefaultPriority & "</priority>",False) Call Logger(conSitemap,vbTab & "</url>",False) Next Call Logger(conSitemap,"</urlset>",False) End If Set objFSO = Nothing End Sub Private Function ConvertLocalPathToUrl(strPath) 'Check If File is Default Document Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") Dim strDefaultDocument Dim blnDefaultDocument: blnDefaultDocument = False If blnDefaultDocumentIsArray = True Then For Each strDefaultDocument in arrDefaultDocument If StrComp(objFSO.GetFileName(strPath),strDefaultDocument,1) = 0 Then blnDefaultDocument = True End If Next End If 'Return Results Dim strResult: strResult = "" strResult = Replace(strPath,conLocalRoot,conWebsiteFQDN,1,1,1) strResult = Replace(strResult,"\","/") If blnDefaultDocument = True Then strResult = Replace(strResult,objFSO.GetFileName(strPath),"") End If ConvertLocalPathToUrl = strResult Set objFSO = Nothing End Function Private Function BubbleSort(ByRef arrValues) Dim j, k, Temp For j = 0 To UBound(arrValues) - 1 For k = j + 1 To UBound(arrValues) If (arrValues(j) > arrValues(k)) Then Temp = arrValues(j) arrValues(j) = arrValues(k) arrValues(k) = Temp End If Next Next BubbleSort = arrValues End Function Private Function GetDate(strDate) 'Ensure Valid Date If Not IsDate(strDate) Then strDate = Date End If Dim strYear: strYear = Year(strDate) Dim strMonth: strMonth = Month(strDate) Dim strDay: strDay = Day(strDate) If strMonth < 10 Then strMonth = 0 & strMonth End If If strDay < 10 Then strDay = 0 & strDay End If GetDate = strYear & "-" & strMonth & "-" & strDay End Function Private Sub Logger(fileName, logMessage, blnNewLog) On Error Resume Next Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") Dim scriptPath: scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,"\")) Dim logName If InStr(1,fileName,"\",1) > 0 Then logName = fileName If objFSO.DriveExists(objFSO.GetDriveName(logName)) Then If StrComp(objFSO.GetExtensionName(logName), "", 1) = 0 Then If Not objFSO.FolderExists(logName) Then If objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then objFSO.CreateFolder logName 'Create Folder In Current Path Exit Sub Else Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog) 'Recurse Creating Parent Folder Call Logger(logName, logMessage, blnNewLog) 'Recurse Creating Current Folder Exit Sub End If End If Else If Not objFSO.FileExists(logName) Then If Not objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog) 'Recurse Creating Parent Folder Call Logger(logName, logMessage, blnNewLog) 'Recurse Creating Current Folder End If End If End If End If Else logName = scriptPath & fileName End If Dim logFile If blnNewLog = True Then Set logFile = objFSO.CreateTextFile(logName, True) Else If objFSO.FileExists(logName) Then Set logFile = objFSO.OpenTextFile(logName, ForAppending, True) Else Set logFile = objFSO.CreateTextFile(logName, True) End If End If logFile.WriteLine logMessage logFile.Close Set objFSO = Nothing On Error Goto 0 End |
Leave a Reply