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 :-).