Quantcast
Viewing all articles
Browse latest Browse all 21975

add a progress bar for each file downloaded

Im downloading a bunch of files using the webclient downloadfileasync method. The url soruce is an access database. I got some really good help doing it in this thread.

The progress bar idea is stolen from here. That helped me alot and im managing to get a progress bar running, but for now i only got one progress bar. For every other file i get and error that says:"Register-ObjectEvent : Cannot subscribe to event. A subscriber with source identifier 'WebClient.DownloadProgressChanged' already exists." 

Here is the full script:

 $adOpenStatic = 3
$adLockOptimistic = 3

$objConnection = New-Object -comobject ADODB.Connection
$objRecordset = New-Object -comobject ADODB.Recordset

$objConnection.Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = d:\powershell\test1.mdb")
$objRecordset.Open("Select * from TBL", $objConnection,$adOpenStatic,$adLockOptimistic)

$objRecordset.MoveFirst()



 while($objrecordset.EOF -ne $true){
 foreach($urls in "url")
 {
 $url = ($objRecordset.Fields.Item("url").Value)
 $filename = "d:\powershell\" + ($objRecordset.Fields.Item("navn").Value) + ".jpg"

 getfile $url $filename

 }
  $objRecordset.MoveNext()


 }

 #######################
   
  
 function getfile($url, $filename)  
  
  {
   $wc = New-Object System.Net.WebClient
   Register-ObjectEvent -InputObject $wc -EventName DownloadProgressChanged -SourceIdentifier WebClient.DownloadProgressChanged -Action { Write-Progress -Activity "Downloading: $($EventArgs.ProgressPercentage)% Completed" -Status $url -PercentComplete $EventArgs.ProgressPercentage; }    
   Register-ObjectEvent -InputObject $wc -EventName DownloadFileCompleted -SourceIdentifier WebClient.DownloadFileComplete -Action { Write-Host "Download Complete - $filename"; Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged; Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete; }  
   try  
   {  
     $wc.DownloadFileAsync($url, $filename)  
   }  
   catch [System.Net.WebException]  
   {  
     Write-Host("Cannot download $url")  
   }   
  finally  
   {    
     $wc.Dispose()  
   }
    }
 $objRecordset.Close()
$objConnection.Close()


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>