PowerShell script to convert a list of URLs into tidy HTML links

by Klaus Graefensteiner 26. August 2010 12:04

Introduction

I recently posted lists of links to Photoshop and CodeIgniter tutorials. I wrote a PowerShell script that takes a text file with URLs as input and creates the html for my blog post from it.

Example

For example the input URL looks like this:

http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-8-ajax/

The output looks like this.

<a href="http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-8-ajax/">Codeigniter from scratch day 8 ajax</a><br/>

The PowerShell script

The script parses the URL and generates the title of the anchor element with simple string operations.

$URLs = Get-Content -Path "CodeIgniterTutorialUrls.txt"

function Get-HTML($URL)
{ 
    $Result = $URL.Replace('-', ' ');
    $Result = $Result -replace  '/$', ''    
    $Result = $Result.Substring($Result.LastIndexOf('/') + 1)
    $FirstLetter = $Result[0].ToString().ToUpper();
    $Text = $FirstLetter + $Result.Substring(1);
    $HTML = '<a href="{0}">{1}</a><br/>' -f $URL, $Text
    return $HTML   
}

Set-Content -Path "LinkHtml.txt" -Value "Links"

foreach($URL in $URLS)
{
    $HTML = Get-HTML $URL
    Add-Content -Path "LinkHtml.txt" -Value $HTML
    $HTML
}

Download

The script and sample data can be downloaded here: URLToHtml.zip

Ausblick

As always PowerShell is the Swiss Army Knife of Windows Cowboys :-).

Tags: , , ,

Blogging | PowerShell | Web Design | Blog Kebab

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2012, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2012 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/20/2012 6:21:08 PM (PST Pacific Standard Time UTC DST -7)