(view source code of sitemap.bat as plain text)
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Enable delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION
:: Check command line
ECHO "%~1" | FINDSTR /R /C:"[\?\*/]" >NUL && GOTO Syntax
ECHO "%~2" | FINDSTR /R /C:"[\?\*\\]" >NUL && GOTO Syntax
IF "%~3"=="" GOTO Syntax
IF NOT EXIST "%~3" GOTO Syntax
:: Initialize some variables we will use later
SET XML=%~1
SET ExcludeList="%~dpn1.xcl"
SET Backup=
SET Attribs=-------
:: If the exlusions list "sitemap.xcl" doesn't exist, we still want our code
:: to work, preferably without having to check if the file exists at each
:: iteration; that's why we'll use NUL as our "alternative" exlusion list
IF NOT EXIST %ExcludeList% SET ExcludeList=NUL
:: Initialize more variables
FOR %%A IN ("%XML%") DO (
SET Output=%%~nxA
IF EXIST %XML% SET Attribs=%%~aA
)
:: Create a numbered backup file if the sitemap already exists; an additional
:: advantage is we can perform a rollback if there was nothing new to index
IF EXIST "%XML%" (
TITLE Backup previous sitemap
FOR /L %%A IN (1001,1,2000) DO (
SET X=%%A
SET X=!X:~1!
IF NOT EXIST "%XML%.!X!" IF EXIST "%XML%" (
SET Backup=%Output%.!X!
REN "%XML%" "!Backup!"
)
)
)
:: Write the XML header
TITLE Creating sitemap
> %XML% ECHO ^<?xml version="1.0" encoding="utf-8"?^>
>> %XML% ECHO ^<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"^>
:Loop
:: Set source folder/filespec and site URL
SET Site=%~2
SET Source=%~3
:: Parse the folder/filespec
ECHO "%~3" | FINDSTR /R /C:"[\?\*]" >NUL
IF ERRORLEVEL 1 (
SET Wildcards=0
FOR /F "tokens=*" %%A IN ("%~3.\..") DO SET Folder=%%~fA
SET Filespec=%~nx3
FOR /D %%A IN ("!Folder!\*.*") DO IF /I "%%~nxA"=="!Filespec!" SET Wildcards=1
IF !Wildcards!==1 (
SET Folder=%~f3
SET Filespec=*.*
)
) ELSE (
SET Wildcards=1
FOR /F "tokens=*" %%A IN ("%~3.\..") DO SET Folder=%%~fA
SET Filespec=%~3
FOR /L %%A IN (1,1,100) DO (
FOR /F "tokens=1* delims=\" %%B IN ("!Filespec!") DO IF NOT "%%~C"=="" SET Filespec=%%~C
)
)
:: Some feedback
ECHO sitemap.bat "%XML%" "%Site%" "%Source%"
SET Folder
SET Filespec
ECHO.
:: Navigate to the source folder
PUSHD "%Folder%"
:: Add all files to the list, except the ones in the exclusions list
FOR %%A IN ("%Filespec%") DO (
TYPE %ExcludeList% | FIND /I "%%~nxA" >NUL
IF ERRORLEVEL 1 (
TITLE %%A
>> %XML% ECHO ^<url^>
>> %XML% ECHO ^<loc^>%Site%%%~nxA^</loc^>
FOR /F "tokens=1-3 delims=- " %%B IN ("%%~tA") DO (>> %XML% ECHO ^<lastmod^>%%D-%%C-%%B^</lastmod^>)
>> %XML% ECHO ^</url^>
)
)
POPD
:: If there are more folders to be indexed, handle the next one
IF NOT "%~4"=="" (
SHIFT
SHIFT
GOTO Loop
)
:: Close the XML file
>> %XML% ECHO ^</urlset^>
:: Check if anything has changed since the last indexing:
:: if so, open the new sitemap for inspection, if not, perform a rollback
IF NOT "%Backup%"=="" (
ECHO N| COMP "%XML%" "%Backup%" 2>&1 | FIND "Files compare OK" >NUL
IF ERRORLEVEL 1 (
START "SiteMap" "%XML%"
) ELSE (
ECHO No changes since last indexing...
DEL "%XML%"
REN "%Backup%" "%Output%"
IF /I NOT "%Attribs:~2,1%"=="a" ATTRIB -A %XML%
)
) ELSE (
START "SiteMap" %XML%
)
ENDLOCAL
POPD
GOTO:EOF
:Syntax
IF "%OS%"=="Windows_NT" ENDLOCAL
IF "%OS%"=="Windows_NT" SETLOCAL DISABLEDELAYEDEXPANSION
ECHO Sitemap.bat, Version 1.00 for Windows 2000 and later
ECHO Create a basic sitemap.org compliant XML sitemap
ECHO.
ECHO Usage: SITEMAP sitemap_file site_url source [ site_url2 source2 [...] ]
ECHO.
ECHO Where: sitemap_file is (the local path to) the XML sitemap to be created
ECHO site_url is the URL of the website (must end with forward slash)
ECHO source the local path/filespec of the files to be indexed
ECHO (if only a folder is specified, *.* is assumed)
ECHO.
ECHO Notes: File NAMES must NOT contain "special" (high ASCII/Unicode) characters!
ECHO You can list file names (no path) to be excluded in the optional
ECHO sitemap.xcl, which must be located in the location of this script.
ECHO.
ECHO Example:
ECHO.========
ECHO Create a sitemap named sitemap.xml in the current directory, for the site
ECHO example.org, with PHP source files in the current directory and a subfolder:
ECHO.
ECHO SITEMAP sitemap.xml http://example.org/ *.php http://example.org/sub/ sub\*.php
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
IF "%OS%"=="Windows_NT" ENDLOCAL
EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0100 seconds