Skip to content

Instantly share code, notes, and snippets.

@pekkavaa
Last active January 25, 2023 02:15
Show Gist options
  • Save pekkavaa/5732454 to your computer and use it in GitHub Desktop.
Save pekkavaa/5732454 to your computer and use it in GitHub Desktop.
This batch script creates an RSS 2.0 XML-file called posts.xml in the current directory, and prompts the user for a message. The message is added to the XML file as a news-feed item. When placed in a Dropbox public folder it can act as a minimal blogging platform, since the feed file can be read with a standard RSS reader application.
@echo OFF
setlocal EnableDelayedExpansion
set "xml_template=^^<?xml version="1.0" encoding="windows-1252"?^^>^^<rss version="2.0"^^>^^<channel^^>^^<title^^>microblog title^^</title^^>^^<description^^>My mikroblog of excellence!^^</description^^>^^</channel^^>^^</rss^^>"
set post_file=posts.xml
if exist %post_file% (
echo Post database %post_file% found.
) else (
echo Creating %post_file%...
echo %xml_template% > %post_file%
if errorlevel 1 goto db_error
echo Please edit %post_file% to add custom title and description.
)
goto :ask_message
:ask_message
goto :post_message
:post_message
set command= ^"$a = New-Object System.Globalization.CultureInfo(\"en-US\"); ^
$message = Read-Host 'New microblog post' ;^
$d = Get-Date; ^
$date = $d.ToString(\"r\", $a); ^
[xml]$xml = Get-Content %post_file%; ^
$newpost = $xml.CreateElement(\"item\"); ^
$content = $xml.CreateElement(\"content\"); ^
$title = $xml.CreateElement(\"title\"); ^
$content.AppendChild($xml.CreateTextNode($message)); ^
$title.AppendChild($xml.CreateTextNode($message)); ^
$pubDate = $xml.CreateElement(\"pubDate\"); ^
$pubDate.AppendChild($xml.CreateTextNode($date)); ^
$newpost.AppendChild($title); ^
$newpost.AppendChild($pubDate); ^
$newpost.AppendChild($content); ^
$xml.rss.channel.PrependChild($newpost); ^
$result = $xml.Save('%post_file%'); ^
^"
powershell.exe -NoProfile -Command %command%
if errorlevel 1 goto post_error
echo Message saved successfully.
pause
goto :eof
:db_error
echo Error occured, aborting.
goto :eof
:post_error
echo Error occured when posting, aborting.
goto :eof
:eof
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment