I have a website which has a section under maintenance with a button called create a new file - when you click on this button it does a backup and generate a filename on the same page called *.tbf - the filename changes each time you click on create a new file

i would like my vb program to click on that button and download the file - now the filename will be different each time, is there a way to find a *.tdf and download it?

the create a new file code is the following:

<input type="button" onclick="fnnOnCreteNewClicked(document.frmAddrBkBackup,'NR');" value="Create New File">

please help - thank you very much

Depending on the structure of the page, you can load it with a WebClient or a HttpWebRequest methodology.
Once you've loaded the page, you can search the HTML for the file name and download it.
If the site does not require you to log in, you can use the WebClient's OpenRead() method to get a stream to the HTML.

Edited 4 Years Ago by thines01: n/a

Here is a sample:

Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Text.RegularExpressions

Module Module1
   Function DownloadFile(ByVal strURI As String, ByRef strError As String) As Boolean
      Dim blnRetVal As Boolean = True
      Try
         Dim strTempDir As String = Path.GetTempPath()
         Dim strTempFile = strURI.Split("/".ToCharArray()).Last()
         With New WebClient()
            .DownloadFile(strURI, Path.Combine(strTempDir, strTempFile))
         End With
      Catch exc As Exception
         blnRetVal = False
         strError = exc.Message
      End Try
      Return blnRetVal
   End Function

   Sub Main()
      ' generic website with files listed to download
      Dim strUri As String = "http://www.textpad.com/download/index.html"
      Dim strData As String = ""
      Dim strTargetFile As String = ""
      Dim wc As New WebClient()

      Dim rxDownloadFile As New Regex("""(?<file>http://download.textpad.com/download/.*/.*.exe)""")

      Try
         Dim fileWebIn As New StreamReader(wc.OpenRead(strUri))
         While Not (fileWebIn.EndOfStream)
            strData = Regex.Replace(fileWebIn.ReadLine(), "[\s]", "")
            If (rxDownloadFile.IsMatch(strData)) Then
               strTargetFile = rxDownloadFile.Match(strData).Groups(1).Value
               Exit While ' break
            End If

         End While
         fileWebIn.Close()
      Catch exc As Exception
         Console.WriteLine("Could not open web page: " + exc.Message)
         Return
      End Try

      Dim strError As String = ""
      If (Not String.IsNullOrEmpty(strTargetFile)) Then
         Console.WriteLine("Downloading file: " + strTargetFile)
         If Not (DownloadFile(strTargetFile, strError)) Then
            Console.WriteLine("Could not download file: " + strError)
         Else
            Console.WriteLine("File is now in the temp directory")
         End If
      End If
   End Sub
End Module
This article has been dead for over six months. Start a new discussion instead.