PoshCode Logo PowerShell Code Repository

Get-WebFile 3.1 (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/172"></script>download | new post

A complete rewrite of my wget script to use HttpWebRequest and Response to figure out the filename, added some unwrapping because a couple sites had quotes around the file names.

  1. ## Get-WebFile.ps1 (aka wget for PowerShell)
  2. ##############################################################################################################
  3. ## Downloads a file or page from the web
  4. ## History:
  5. ## v3.1 - Unwrap the filename when it has quotes around it
  6. ## v3   - rewritten completely using HttpWebRequest + HttpWebResponse to figure out the file name, if possible
  7. ## v2   - adds a ton of parsing to make the output pretty
  8. ##      - added measuring the scripts involved in the command, (uses Tokenizer)
  9. ##############################################################################################################
  10. #function wget {
  11.    param(
  12.       $url = (Read-Host "The URL to download"),
  13.       $fileName
  14.    )
  15.    
  16.    $req = [System.Net.HttpWebRequest]::Create($url);
  17.    $res = $req.GetResponse();
  18.  
  19.    if($fileName -and !(Split-Path $fileName)) {
  20.       $fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
  21.    }
  22.    elseif(($fileName -eq $null) -or (Test-Path -PathType "Container" $fileName))
  23.    {
  24. #  if( -and !((Test-Path -PathType "Leaf" $fileName) -or ((Test-Path -PathType "Container" (Split-Path $fileName)) -and -not )))
  25.       [string]$fileName = ([regex]'(?i)filename=(.*)$').Match( $res.Headers["Content-Disposition"] ).Groups[1].Value
  26.       $fileName = $fileName.trim("\/""'")
  27.       if(!$fileName) {
  28.          $fileName = $res.ResponseUri.Segments[-1]
  29.          $fileName = $fileName.trim("\/")
  30.          if(!$fileName) {
  31.             $fileName = Read-Host "Please provide a file name"
  32.          }
  33.          $fileName = $fileName.trim("\/")
  34.          if(!([IO.FileInfo]$fileName).Extension) {
  35.             $fileName = $fileName + "." + $res.ContentType.Split(";")[0].Split("/")[1]
  36.          }
  37.       }
  38.       $fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
  39.    }
  40.  
  41.    if($res.StatusCode -eq 200) {
  42.       $reader = new-object System.IO.StreamReader $res.GetResponseStream()
  43.       $writer = new-object System.IO.StreamWriter $fileName
  44.       # TODO: stick this in a loop and give progress reports
  45.       $writer.Write($reader.ReadToEnd())
  46.      
  47.       $reader.Close();
  48.       $writer.Close();
  49.    }
  50.    $res.Close();
  51.    ls $fileName
  52. #}

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me