Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sitemap.bat

(view source code of sitemap.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Enable delayed variable expansion
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check command line
  9. ECHO "%~1" | FINDSTR /R /C:"[\?\*/]"  >NUL && GOTO Syntax
  10. ECHO "%~2" | FINDSTR /R /C:"[\?\*\\]" >NUL && GOTO Syntax
  11. IF "%~3"=="" GOTO Syntax
  12. IF NOT EXIST "%~3" GOTO Syntax
  13.  
  14. :: Initialize some variables we will use later
  15. SET XML=%~1
  16. SET ExcludeList="%~dpn1.xcl"
  17. SET Backup=
  18. SET Attribs=-------
  19.  
  20. :: If the exlusions list "sitemap.xcl" doesn't exist, we still want our code
  21. :: to work, preferably without having to check if the file exists at each
  22. :: iteration; that's why we'll use NUL as our "alternative" exlusion list
  23. IF NOT EXIST %ExcludeList% SET ExcludeList=NUL
  24.  
  25. :: Initialize more variables
  26. FOR %%A IN ("%XML%") DO (
  27. 	SET Output=%%~nxA
  28. 	IF EXIST %XML% SET Attribs=%%~aA
  29. )
  30.  
  31. :: Create a numbered backup file if the sitemap already exists; an additional
  32. :: advantage is we can perform a rollback if there was nothing new to index
  33. IF EXIST "%XML%" (
  34. 	TITLE Backup previous sitemap
  35. 	FOR /L %%A IN (1001,1,2000) DO (
  36. 		SET X=%%A
  37. 		SET X=!X:~1!
  38. 		IF NOT EXIST "%XML%.!X!" IF EXIST "%XML%" (
  39. 			SET Backup=%Output%.!X!
  40. 			REN "%XML%" "!Backup!"
  41. 		)
  42. 	)
  43. )
  44.  
  45. :: Write the XML header
  46. TITLE Creating sitemap
  47. >  %XML% ECHO ^<?xml version="1.0" encoding="utf-8"?^>
  48. >> %XML% ECHO ^<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"^>
  49.  
  50. :Loop
  51. :: Set source folder/filespec and site URL
  52. SET Site=%~2
  53. SET Source=%~3
  54.  
  55. :: Parse the folder/filespec
  56. ECHO "%~3" | FINDSTR /R /C:"[\?\*]" >NUL
  57. IF ERRORLEVEL 1 (
  58. 	SET Wildcards=0
  59. 	FOR /F "tokens=*" %%A IN ("%~3.\..") DO SET Folder=%%~fA
  60. 	SET Filespec=%~nx3
  61. 	FOR /D %%A IN ("!Folder!\*.*") DO IF /I "%%~nxA"=="!Filespec!" SET Wildcards=1
  62. 	IF !Wildcards!==1 (
  63. 		SET Folder=%~f3
  64. 		SET Filespec=*.*
  65. 	)
  66. ) ELSE (
  67. 	SET Wildcards=1
  68. 	FOR /F "tokens=*" %%A IN ("%~3.\..") DO SET Folder=%%~fA
  69. 	SET Filespec=%~3
  70. 	FOR /L %%A IN (1,1,100) DO (
  71. 		FOR /F "tokens=1* delims=\" %%B IN ("!Filespec!") DO IF NOT "%%~C"=="" SET Filespec=%%~C
  72. 	)
  73. )
  74.  
  75. :: Some feedback
  76. ECHO sitemap.bat "%XML%" "%Site%" "%Source%"
  77. SET Folder
  78. SET Filespec
  79. ECHO.
  80.  
  81. :: Navigate to the source folder
  82. PUSHD "%Folder%"
  83. :: Add all files to the list, except the ones in the exclusions list
  84. FOR %%A IN ("%Filespec%") DO (
  85. 	TYPE %ExcludeList% | FIND /I "%%~nxA" >NUL
  86. 	IF ERRORLEVEL 1 (
  87. 		TITLE %%A
  88. 		>> %XML% ECHO   ^<url^>
  89. 		>> %XML% ECHO     ^<loc^>%Site%%%~nxA^</loc^>
  90. 		FOR /F "tokens=1-3 delims=- " %%B IN ("%%~tA") DO (>> %XML% ECHO     ^<lastmod^>%%D-%%C-%%B^</lastmod^>)
  91. 		>> %XML% ECHO   ^</url^>
  92. 	)
  93. )
  94. POPD
  95.  
  96. :: If there are more folders to be indexed, handle the next one
  97. IF NOT "%~4"=="" (
  98. 	SHIFT
  99. 	SHIFT
  100. 	GOTO Loop
  101. )
  102.  
  103. :: Close the XML file
  104. >> %XML% ECHO ^</urlset^>
  105.  
  106. :: Check if anything has changed since the last indexing:
  107. :: if so, open the new sitemap for inspection, if not, perform a rollback
  108. IF NOT "%Backup%"=="" (
  109. 	ECHO N| COMP "%XML%" "%Backup%" 2>&1 | FIND "Files compare OK" >NUL
  110. 	IF ERRORLEVEL 1 (
  111. 		START "SiteMap" "%XML%"
  112. 	) ELSE (
  113. 		ECHO No changes since last indexing...
  114. 		DEL "%XML%"
  115. 		REN "%Backup%" "%Output%"
  116. 		IF /I NOT "%Attribs:~2,1%"=="a" ATTRIB -A %XML%
  117. 	)
  118. ) ELSE (
  119. 	START "SiteMap" %XML%
  120. )
  121. ENDLOCAL
  122. POPD
  123. GOTO:EOF
  124.  
  125.  
  126. :Syntax
  127. IF "%OS%"=="Windows_NT" ENDLOCAL
  128. IF "%OS%"=="Windows_NT" SETLOCAL DISABLEDELAYEDEXPANSION
  129. ECHO Sitemap.bat,  Version 1.00 for Windows 2000 and later
  130. ECHO Create a basic sitemap.org compliant XML sitemap
  131. ECHO.
  132. ECHO Usage:   SITEMAP  sitemap_file  site_url  source  [ site_url2  source2 [...] ]
  133. ECHO.
  134. ECHO Where:   sitemap_file   is (the local path to) the XML sitemap to be created
  135. ECHO          site_url       is the URL of the website (must end with forward slash)
  136. ECHO          source         the local path/filespec of the files to be indexed
  137. ECHO                         (if only a folder is specified, *.* is assumed)
  138. ECHO.
  139. ECHO Notes:   File NAMES must NOT contain "special" (high ASCII/Unicode) characters!
  140. ECHO          You can list file names (no path) to be excluded in the optional
  141. ECHO          sitemap.xcl, which must be located in the location of this script.
  142. ECHO.
  143. ECHO Example:
  144. ECHO.========
  145. ECHO Create a sitemap named sitemap.xml in the current directory, for the site
  146. ECHO example.org, with PHP source files in the current directory and a subfolder:
  147. ECHO.
  148. ECHO SITEMAP sitemap.xml http://example.org/ *.php http://example.org/sub/ sub\*.php
  149. ECHO.
  150. ECHO Written by Rob van der Woude
  151. ECHO http://www.robvanderwoude.com
  152.  
  153. IF "%OS%"=="Windows_NT" ENDLOCAL
  154. EXIT /B 1
  155.  

page last modified: 2024-02-26; loaded in 0.0238 seconds