Results 1 to 9 of 9

Thread: Download a list of files and report Progress

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Cool Download a list of files and report Progress

    Some people might find this usefull as much as i have so here it is. This code downloads a list of files from a txt file to the directory you set it to. Just add a button and a progressbar (change its name to Progbar) and your good to go, enjoy

    vb.net Code:
    1. Imports System.IO
    2. Imports System.Net
    3. Imports Microsoft.VisualBasic.FileIO.FileSystem
    4.  
    5. Public Class Form1
    6.     Private Const sfilename As String = "c:\Users\****\Desktop\list_of_files.txt"
    7.     Private Const url As String = "http://www.***.com/files"
    8.     Private Const spath As String = "c:\Users\****\Desktop\test"
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         'CHECK FOR LIST
    11.         If Not My.Computer.FileSystem.FileExists(sfilename) Then
    12.             MsgBox("No list found, exiting application", MsgBoxStyle.Critical)
    13.             Application.Exit()
    14.         End If
    15.         Dim sr As New IO.StreamReader(sfilename)
    16.         Dim line As String = sr.ReadLine()
    17.         Dim req As Net.WebRequest
    18.         Dim resp As IO.Stream
    19.         Dim out As IO.BinaryWriter
    20.         Do While line IsNot Nothing
    21.             req = Net.HttpWebRequest.Create(url & line)
    22.             Dim progmax As Net.HttpWebResponse = req.GetResponse()
    23.             resp = req.GetResponse().GetResponseStream()
    24.             '===========================
    25.             'CREATES DIRECTORY IF NEEDED BEFORE IT CALLS TO OPEN A NEW FILESTREAM AND THERE IS NONE
    26.             '===========================
    27.             Dim filepath As String = spath & line
    28.             If Not Directory.Exists(filepath.Replace(Path.GetFileName(filepath), String.Empty)) Then Directory.CreateDirectory(filepath.Replace(Path.GetFileName(filepath), String.Empty))
    29.             '===========================
    30.             out = New IO.BinaryWriter(New IO.FileStream(spath & line, IO.FileMode.OpenOrCreate))
    31.             Dim buf(4096) As Byte
    32.             Dim k As Int32 = resp.Read(buf, 0, 4096)
    33.             Do While k > 0
    34.                 out.Write(buf, 0, k)
    35.                 k = resp.Read(buf, 0, 4096)
    36.                 '===================
    37.                 'READ FROM THE BUFFER AND REPORT PROGRESS TO THE PROGBAR
    38.                 '===================
    39.                 Progbar.Maximum = progmax.ContentLength
    40.                 If Progbar.Value + k <= Progbar.Maximum Then
    41.                     Progbar.Value += k
    42.                 Else
    43.                     Progbar.Value = Progbar.Maximum
    44.                 End If
    45.             Loop
    46.             resp.Close()
    47.             out.Close()
    48.             line = sr.ReadLine()
    49.         Loop
    50.     End Sub
    51. End Class
    Attached Files Attached Files
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: Download a list of files and report Progress

    Note: Currently as the code is structured, you MUST have all the files on the same server.

    As for your list, it was structured like this when the code was written so that when the lines are merged and called to be downloaded there would be no confusion.

    Code:
    /1994945_qcmrb/RAR_TEST_124014.rar
    /1993519_otmvd/RAR_TEST_121453.rar
    /1993197_dlfm1/RAR_TEST.rar
    /1991215_gxosq/Unrar.dll
    /1990977_ghj15/Test_Multithreading_10.24.08_065000.rar
    /1973022_ylvfo/Patch.rar
    So it would download like this "http://www.***.com/files" & "/1994945_qcmrb/RAR_TEST_124014.rar"
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Download a list of files and report Progress

    Please include your attached files in a zip file rather than a .rar file.

    Everyone has Winzip...not everyone has the ability to open a .rar file.

    Thanks.

  4. #4
    Addicted Member
    Join Date
    Jan 2008
    Posts
    162

    Re: Download a list of files and report Progress

    Nice app any change to see what file is downloading in textbox? Please for example ? Thx

  5. #5
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: Download a list of files and report Progress

    so what does this download ?

  6. #6
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Re: Download a list of files and report Progress

    Tweaker99:- It downloads anything you want assuming you have the Location of the file on the web...

    michalss:- Simply add a line just before 'Do While k > 0' like below

    Code:
    If Not Directory.Exists(filepath.Replace(Path.GetFileName(filepath), String.Empty)) Then Directory.CreateDirectory(filepath.Replace(Path.GetFileName(filepath), String.Empty))
                out = New IO.BinaryWriter(New IO.FileStream(spath & line, IO.FileMode.OpenOrCreate))
                Dim buf(4096) As Byte
                Dim k As Int32 = resp.Read(buf, 0, 4096)
                TextBox1.Text = TextBox1.Text & vbNewLine & "-> Downloading " & line '<<< Line is the File, This will add a new line saying what it is downloading each time the file changes
                TextBox1.Refresh()   '<<< Refresh the Textbox so it shows it
                Do While k > 0
                    out.Write(buf, 0, k)
                    k = resp.Read(buf, 0, 4096)
                    'READ FROM THE BUFFER AND REPORT PROGRESS TO THE PROGBAR
    Now for my Question How can i calculate the Transfer Rate from this?

    Cheers
    -Tom
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: Download a list of files and report Progress

    Sorry for the late reply. I've been really inactive as ive been tied up in a big project the past months. Heres a link to where it shows you can add the download speed (im assuming thats what you mean by transfer rate?) http://www.codeproject.com/KB/vb/dow...ogressbar.aspx
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  8. #8
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Re: Download a list of files and report Progress

    Fantastic

    Cheers
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: Download a list of files and report Progress

    No prob. You just need one block from that and you have your download speed
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width