Sunday, November 18, 2012

Downloading files in powershell

I used to download files in batch files using  wget or curl. Now that I have "the powershell" I can do better, by instantiating a WebClient object.   Here's my example dependency downloader for my coffee script unit tests.


$filesToDownload = (
"http://jashkenas.github.com/coffee-script/extras/coffee-script.js",
"https://raw.github.com/jquery/qunit/master/qunit/qunit.js",
"https://raw.github.com/jquery/qunit/master/qunit/qunit.css"
)
$webClient = new-object System.Net.WebClient

$filesToDownload | % { 
    $uri = new-Object System.Uri $_ ; 
    $localPath =  "$($pwd.Path)\$($uri.Segments[-1])"; 
    Write-Host "Writing $localPath" ;
    $webClient.DownloadFile($uri,$localPath); 
}

No comments: