param( [Parameter(Mandatory=$true)] [string]$BaseUrl, [Parameter(Mandatory=$true)] [string]$RootPath, [string]$OutputPath = "$RootPath\sitemap.xml", [switch]$IncludeLastMod, [switch]$IncludeChangeFreq, [switch]$IncludePriority ) # Normalize inputs $BaseUrl = $BaseUrl.TrimEnd('/') $RootPath = (Resolve-Path -Path $RootPath).ProviderPath.TrimEnd('\') if (-not (Test-Path -Path $RootPath -PathType Container)) { Write-Error "RootPath '$RootPath' does not exist or is not a directory." exit 1 } # Gather all .html/.htm files recursively $files = Get-ChildItem -Path $RootPath -Recurse -File -Include *.html, *.htm if ($files.Count -eq 0) { Write-Warning "No .html or .htm files found under '$RootPath'." exit 0 } # Setup XML writer $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.Encoding = [System.Text.Encoding]::UTF8 $writer = [System.Xml.XmlWriter]::Create($OutputPath, $settings) $writer.WriteStartDocument() $writer.WriteStartElement('urlset') $writer.WriteAttributeString('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9') foreach ($file in $files) { # Build relative URL path $relativePath = $file.FullName.Substring($RootPath.Length).TrimStart('\','/') $relativeUrl = $relativePath -replace '\\','/' # Special handling for default documents (e.g., index.html) if ($relativeUrl -match '^(.*?)(index\.html?|default\.aspx?)$') { $relativeUrl = $Matches[1] } $loc = "$BaseUrl/$relativeUrl".TrimEnd('/') $writer.WriteStartElement('url') $writer.WriteElementString('loc', $loc) if ($IncludeLastMod) { $lastmod = $file.LastWriteTimeUtc.ToString('yyyy-MM-ddTHH:mm:ssZ') $writer.WriteElementString('lastmod', $lastmod) } if ($IncludeChangeFreq) { # You could make this configurable; defaulting to 'weekly' $writer.WriteElementString('changefreq', 'weekly') } if ($IncludePriority) { # Simple heuristic: depth determines priority $depth = ($relativeUrl -split '/').Count $priority = [Math]::Round(1.0 / $depth, 2) if ($priority -lt 0.1) { $priority = 0.1 } $writer.WriteElementString('priority', $priority.ToString('0.0')) } $writer.WriteEndElement() # } $writer.WriteEndElement() # $writer.WriteEndDocument() $writer.Flush() $writer.Close() Write-Host "Sitemap generated successfully at: $OutputPath"