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 at Rockwell Automation and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Administration

About

Powered by:
BlogEngine.Net
Version: 1.5.0.7

License:
Creative Commons License

Copyright:
© Copyright 2009, 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 2009 by Klaus Graefensteiner

Rendertime:
Page rendered at 9/9/2010 6:35:02 PM (PST Pacific Standard Time UTC DST -7)