Skip to content

Instantly share code, notes, and snippets.

@ichensky
Created June 2, 2013 20:53
Show Gist options
  • Save ichensky/5694934 to your computer and use it in GitHub Desktop.
Save ichensky/5694934 to your computer and use it in GitHub Desktop.
Download group of files from ex.ua
Add-Type -Assembly System.Web
$FullFileName = "C:\Users\D\Desktop\15462919.urls.urls"
$FullFolderName = "C:\Users\D\Desktop\File\"
function originalFileName($originalURI)
{
$indexOfSlash = $response.ResponseUri.OriginalString.LastIndexOf("/")
$originalName = $originalURI.Remove(0, $indexOfSlash + 1).Replace("-", "")
$originalName = [System.Web.HttpUtility]::UrlDecode([System.Web.HttpUtility]::UrlDecode([System.Web.HttpUtility]::UrlDecode([System.Web.HttpUtility]::UrlDecode($originalName))))
$originalName = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::GetEncoding("iso-8859-1").GetBytes($originalName))
return $originalName
}
function downloadFile($url)
{
echo "Downloading: " $url
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$originalFileName = originalFileName($response.ResponseUri.OriginalString)
echo "Original file name: " $originalFileName
$targetfile = $FullFolderName + $originalFileName
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetfile, Create
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0)
{
[System.Console]::CursorLeft = 0
[System.Console]::Write("Downloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength)
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
}
"`nFinished Download"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
$f = [System.IO.File]::OpenText($FullFileName)
while (!$f.EndOfStream) {
$line = $f.ReadLine()
downloadFile($line)
}
$f.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment