comment 0

An Afternoon of Coding – Blogger, XML, TextPad and a Batch File

I have almost 600 blog posts (old blog). I have a page that lists out a link to each blog post in chronological order. This is maintained manually. Everytime I publish a blog post, I add it to the list. Today I realised that this list was incorrect as the post count did not match the number of items on this list. I decided that I was going to regenerate this listing using an export of the blog.

Sounds simple but there were some tricks I had to use. The blog export is a big xml file with all the blog data. Included in there is the title and blog post link. I started to think what should I use to extract a list of title and URL. I thought about downloading an XML reader or using PHP or using the findstr command or using TextPad. I decided on TextPad for convenience as I use it on a daily basis. With TextPad I could find and replace using regular expressions. So, I put the title and URL on a new line and then stripped off unneeded information at the beginning and end of the lines. At some point in time I realised that the quotation character in some titles were replaced with gibberish. I fixed this by opening the blog export in NotePad and then copy and pasting from there into TextPad instead of opening directly in TextPad.

The problem I now faced was that title was coming before URL and I would not be able to use TextPad to generate the final HTML. I decided to create a batch file to do this bit. This is the code I came up with. Learnt a few things in the process. Syntax is pretty strick like else and brackets must be on the same line. Less than and greater than must be escaped. Delayed expansion must be used if I wanted the variable in the loop to be evaluated at execution time.

@echo off
setlocal enabledelayedexpansion
set result=
echo ^<ul^> >> out.html
for /f "tokens=*" %%A in (results.txt) do (
 if not "!result!" == "" (
  echo ^	<li^>^<a href="%%A">!result!^<^/a^>^<^/li^> >> out.html
  set result=
 ) else (
  set result=%%A
 )
)
echo ^<^/ul^> >> out.html</a>

Stuff like this I like. I get to utilise my coding and IT background and available tools to solve a problem. I’m documenting what I did here for future reference and to help anyone looking to do similar.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s