Results 1 to 6 of 6

Thread: [RESOLVED] how to download multiple text files from website

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Location
    Providence, RI
    Posts
    14

    Resolved [RESOLVED] how to download multiple text files from website

    I am looking for some suggestions at this point and I was wondering what type of routine I would have to create to download each of these text files individually from a website. I currently can download each new file at this point and name the text file and time stamp it but I was wondering how I could retroactively do this. I was thinking I would have to try to loop through each day, month, and year combination from 20120901 to the present day or is there a way that I can harvest the names off of the website? I put my code on here for downloading the textfile and was planning on setting up a timer to automatically perform this routine. If you see anything that could be optimized in this code let me know I'm pretty new at this and at this point am still trying to figure things out. Thanks!!

    Code:
    Dim FileEndDate As Integer = DateTime.Now.ToString("yyyyMMdd")
    
    
    
            My.Computer.Network.DownloadFile(New Uri("http://www.website.com/bounces/Non%20BounceMessages_" & FileTodayDate & ".txt"), "C:\work\Read\Unwritten\" & " Bounces__" & FileDate & ".txt")
            MsgBox("file downloaded")
            Me.Close(
    Name:  bouncespic.jpg
Views: 683
Size:  116.5 KB
    Last edited by ktolso03; Mar 25th, 2014 at 02:28 PM.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: how to download multiple text files from website

    grab each url that ends in .txt, i wont provide an example with out the sites url.
    My Github - 1d3nt

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Location
    Providence, RI
    Posts
    14

    Re: how to download multiple text files from website

    Quote Originally Posted by ident View Post
    grab each url that ends in .txt, i wont provide an example with out the sites url.
    http://www.promotioncorner.com/bounc...s_20120905.txt
    Last edited by ktolso03; Mar 26th, 2014 at 07:43 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Location
    Providence, RI
    Posts
    14

    Re: how to download multiple text files from website

    I get that your saying something along the lines of this (maybe)
    Code:
     "http://www.promotioncorner.com/bounces/*.txt"
    to download all of the text files present but then to name each file downloaded with the file downloaded to me is a challenge. I keep getting an overflow error with what I have which is just trying to grab the last 12 characters of the name of the file downloaded(kind of a shot in the dark).

  5. #5
    Addicted Member
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    142

    Re: how to download multiple text files from website

    Quote Originally Posted by ktolso03 View Post
    I get that your saying something along the lines of this (maybe)
    Code:
     "http://www.promotioncorner.com/bounces/*.txt"
    to download all of the text files present but then to name each file downloaded with the file downloaded to me is a challenge. I keep getting an overflow error with what I have which is just trying to grab the last 12 characters of the name of the file downloaded(kind of a shot in the dark).

    This code will download all the files listed on that web page you linked to. It's probably not exactly what you want but I think it will get you there.

    Name:  Screenshot (77).png
Views: 514
Size:  5.4 KB

    Requirement: VS2012 and up and .NET 4.5 and up. only because I am using Async and Await
    Create a Windows Forms Application and add a form named Form1 and add 2 ProgressBar named ProgressBar1 and ProgressBar2 and a Label named Label1
    replace the code for Form1 with the following example:

    Change the const SAVE_FILE_FOLDER to the folder you want to save the 500+ text files

    vb.net Code:
    1. Imports System.Net
    2. Imports System.Text.RegularExpressions
    3.  
    4. Public Class Form1
    5.     ' Change this to your own path
    6.     Private Const SAVE_FILE_FOLDER As String = "c:\temp\promotioncornor\"
    7.  
    8.     Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    9.  
    10.         Dim wc As New WebClient
    11.         Dim htmlContent As String = Nothing
    12.         AddHandler wc.DownloadProgressChanged, Sub(s, args)
    13.                                                    Me.ProgressBar2.Value = args.ProgressPercentage
    14.                                                End Sub
    15.  
    16.         Try
    17.             htmlContent = Await wc.DownloadStringTaskAsync("http://www.promotioncorner.com/bounces")
    18.         Catch ex As Exception
    19.             MessageBox.Show(ex.Message)
    20.         End Try
    21.  
    22.         If htmlContent Is Nothing Then Return
    23.         Dim count = 0
    24.         Try
    25.             ' This is web scraping and will possibly break if the website changes the format of the html.
    26.             Dim RegexObj As New Regex("<*""(?<path>[^""]*txt)"">(?<fileName>[^<]*)</A>")
    27.             Dim MatchResults As Match = RegexObj.Match(htmlContent)
    28.             Dim matchCount = RegexObj.Matches(htmlContent).Count
    29.             Me.ProgressBar1.Maximum = matchCount
    30.             While MatchResults.Success
    31.                 count += 1
    32.                 Me.ProgressBar1.Value = count
    33.                
    34.                 Dim fileUrl As String = "http://www.promotioncorner.com" & MatchResults.Groups("path").Value
    35.  
    36.                 Dim fileName As String = MatchResults.Groups("fileName").Value
    37.  
    38.                 Dim fileAndPath = IO.Path.Combine(SAVE_FILE_FOLDER, fileName)
    39.  
    40.                 If Not My.Computer.FileSystem.FileExists(fileAndPath) Then
    41.                     Me.Label1.Text = String.Format("Downloading {0} of {1} files", count, matchCount)
    42.                     ' async download text file.
    43.                     Dim fileContent As String = Await wc.DownloadStringTaskAsync(fileUrl)
    44.  
    45.                     ' async save text file
    46.                     Using writer As New System.IO.StreamWriter(fileAndPath, False)
    47.                         Await writer.WriteAsync(fileContent)
    48.                     End Using
    49.                 Else
    50.                     Me.Label1.Text = String.Format("Skipping {0} of {1} files", count, matchCount)
    51.                     Await Task.Delay(50)
    52.                 End If
    53.  
    54.                 MatchResults = MatchResults.NextMatch()
    55.             End While
    56.         Catch ex As ArgumentException
    57.             MessageBox.Show(ex.Message)
    58.         Finally
    59.             Me.Label1.Text = "Done!"
    60.         End Try
    61.     End Sub
    62.  
    63. End Class
    Last edited by OICU812; Mar 26th, 2014 at 02:19 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Location
    Providence, RI
    Posts
    14

    Re: how to download multiple text files from website

    Thanks so much, your code works great!!!

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