2

I'm using following script to download files using powershell.

 $folder = "c:\temp\"
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 
Firefox/7.0.1"
$web = New-Object System.Net.WebClient
$web.Headers.Add("user-agent", $userAgent)


Get-Content "c:\temp\URL_List.txt" |
Foreach-Object { 
"Downloading " + $_
try {
    $target = join-path $folder ([io.path]::getfilename($_))
    $web.DownloadFile($_, $target)
} catch {
    $_.Exception.Message
}

}

The URL_List.txt file has list of URL's I'm trying to download files from. Here's a sample URL from the list: https://drive.google.com/uc?export=download&id=0B84LPHCa2YmdZmFMV0dsYl9FeTg If you look at the URL there's no absolute file name in the URL so I'm not sure how to set the target parameter for WebClient.DownloadFile() method.

  • 1
    Do you have any way to derive the filename after it's downloaded? You can use `[System.IO.Path]::GetTempFileName()` to generate a tmp file to write the request into until you can figure out its name. Also, what version of PowerShell? You should utilize `Invoke-WebRequest -OutFile` instead of manual .net manipulation (as a best-practice) – Maximilian Burszley Jun 15 '18 at 16:57

2 Answers2

0

Read the Download Files. Also might be worth checking since you're using Powershell, Google Drive REST Api module for Powershell.

Adi Mabfalin
  • 316
  • 2
  • 3
  • 12
0

So the question as I understand it is how to extract the filename for a Google Drive file without downloading the file first

This Chilkat page gave me the idea that it should be possible to access properties with a GET request. Chilkat is a paid API, so I thought I'd try to cobble together a method using direct PowerShell commands.

Invoke-WebRequest works but downloads the whole file. We only need the headers.

This site has the core code for that. From there, it's just parsing the Content-Disposition header to extract the "filename" (and not "filename*"):

$WebPath = "https://drive.google.com/uc?export=download&id=0B84LPHCa2YmdZmFMV0dsYl9FeTg"
$request = [System.Net.WebRequest]::Create( $WebPath ) 
$headers = $request.GetResponse().Headers 
# Content-disposition includes a name-value pair for filename:
$cd = $headers.GetValues("Content-Disposition")
$cd_array = $cd.split(";")
foreach ($item in $cd_array) { 
  if ($item.StartsWith("filename=")) {
      # Get string after equal sign
      $filename = $item.Substring($item.IndexOf("=")+1)
      # Remove quotation marks, if any
      $filename = $filename.Replace('"','')
  }
}
Mark Berry
  • 17,843
  • 4
  • 58
  • 88