I was planning to learn Powershell for some time now and this seemed like a good opportunity to learn a few basic things: we had a txt file with urls of multiple images (one per line) and needed to download them while preserving directory structure. Below is the powershell script with comments. Things that it illustrates:
- Using System.Net.WebClient
- Join-Path helper
- 2.0 error handling using try/catch
$webclient = New-Object System.Net.WebClient $dest = "D:\temp" $errors = @() $images = (get-content $dest"\images.txt") for ($i=0; $i -le $images.Length – 1; $i++) { $url = $images[$i] # DownloadFile doesn't like spaces $url = $url.toString().trim(); # also requires "http://" for URL if ($url.indexOf("http://") -eq -1) { $url = "http://" + $url } Write-Host ($i + 1) " / " $images.Length " : " $url if ($url -ne "") { # Example URL: http://www.example.com/aaaa/bbb/image.jpg # requirement was to preserve the folder structure # removing http:// $str = $url.replace("http://", "") # www.example.com/aaaa/bbb/image.jpg # removing domain name $str = $str.substring($str.indexOf("/")) # /aaaa/bbb/image.jpg $str = $str.replace("/", "\") # \aaa\bbb\image.jpg # determining folder structure by removing file name $dir = $str.substring(0, $str.lastIndexOf("\")) # \aaaa\bbb # if folder doesn't exist locally, create it $imageDir = Join-Path $dest $dir if (!(test-path $imageDir -pathtype container)) { # md create full folder strucutre if provided. # Example: D:\A\B\C\D will create all subfolders md $imageDir } $localImagePath = Join-Path $dest $str Write-Host $localImagePath #http://stackoverflow.com/questions/2182666/powershell-2-0-try-catch-how-to-access-the-exception try { $webclient.DownloadFile($url, $localImagePath) } catch [Exception] { $errors += $url Write-Host "ERROR: " + $_.Exception.ToString() -foregroundcolor "red" } } } Write-Host "done. errors: " $errors