Wednesday, April 23, 2008

Download List of Files from Web with Powershell Script

I recently came across Scott Hanselman's Hansel Minutes and it looked like the sort of thing that I wanted to listen to. At the time I found it the archives had 109 podcasts. I wanted to download them all and stick them on my MP3 player but didn't want to click through to each link and repeat myself 109 times. I'm a big fan of the DRY principal. So I wrote a Powershell script to do it for me.

First off I examined the file naming pattern:

http://perseus.franklins.net/hanselminutes_0001.wma

to

http://perseus.franklins.net/hanselminutes_0109.wma

Fantastically simple. You couldn't ask for a nicer pattern.

So here's the script:

function main()
{
    $clnt = new-object System.Net.WebClient

    $sourceNames = 1..109 |%{"hanselminutes_{0:0000}.wma" -f $_}
    foreach($s in $sourceNames)
    {
        $url = "http://perseus.franklins.net/" + $s
        $target = "c:\temp\" + $s
        write-host 'transfering from' $url 'to' $target
        $clnt.DownloadFile($url, $target)
    }
}

main

 

During the execution of the script over the 109 files that were in the archive at the time 3 files failed because of a timeout and 2 files failed because they didn't follow the naming pattern: The interviews with Jonathan Zuck and Robert Pickering (#'s 86 and 76 respectively) had their names tagged on to the file names.

No comments:

Post a Comment