Results 1 to 2 of 2

Thread: [RESOLVED] FTPWebRequest not returning right files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    20

    Resolved [RESOLVED] FTPWebRequest not returning right files

    Hi,

    I am working on a program and not getting the right files back. I am using the FtpWebRequest class and everything is working but like I said it's not returning the right files. Here is what I have.

    Code:
    Private Sub frmInput_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Const FTPsite As String = "ftp://ftp.dor.in.gov/mcsheriff/dorout"
            Dim reader As StreamReader = Nothing
            Dim fileStream As FileStream = Nothing
            Dim responseStream As Stream = Nothing
            Dim strfile As String = ""
            Dim strtemp As String = ""
            Dim datetemp As DateTime = DateTime.Now.AddYears(-50)
    
            Try
                'This creates a webrequest at the ftp://ftp.dor.in.gov/mcsheriff/dorout
                Dim listRequest As FtpWebRequest = WebRequest.Create("ftp://ftp.dor.in.gov/mcsheriff/dorout")
                'Put a bypass cache request in here so it always gets the most recent data
                'Dim policy As New Cache.RequestCachePolicy(Cache.RequestCacheLevel.BypassCache)
                'listRequest.CachePolicy = policy
                'gets all the results and puts it in a list in HTML form
                listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
                'This gets the username and password of the requested ftpwebrequest
                listRequest.Credentials = New NetworkCredential("######", "######")
                'creates a getresponse as a ftpwebresponse
                Dim listResponse As FtpWebResponse = listRequest.GetResponse()
                'puts the response in a stream reader so it reads through every line
                reader = New StreamReader(listResponse.GetResponseStream())
    
                'This do loop reads the html file that it creates and start reading files at <PRE> and stops at </PRE>
                'It also looks as the files that has TOTALS in them and disregards them
                Do While Not reader.EndOfStream
                    strtemp = reader.ReadLine
                    If strtemp = "<PRE>" Then
                        Do
                            strtemp = reader.ReadLine()
                            If strtemp = "</PRE>" Then Exit Do
                            Dim sections() As String
                            sections = Split(strtemp, """")
                            'makes sure that there isn't the word TOTALS in the file as we don't need the TOTALS files
                            If InStr(sections(1), "TOTALS") = 0 Then
                                'reads the dates of every file that is passed into the file within the <PRE> </PRE> block
                                'Splits the files into sections
                                If CDate(Microsoft.VisualBasic.Left(sections(0), 17)) > datetemp Then
                                    datetemp = CDate(Microsoft.VisualBasic.Left(sections(0), 17))
                                    strfile = FTPsite & "/" & sections(1)
                                End If
                                Console.WriteLine(strtemp)
                            End If
                        Loop
                    End If
                Loop
    
                Console.WriteLine(strfile)
                'this calls the DownloadFile sub so the file that is selected can be downloaded to the right file path
                If DownloadFile(strfile) Then
                    TextBox1.Text &= strfile & "  ---  Downloaded  ---" & vbCrLf
                End If
    
                'Creates error messages if something is wrong
            Catch ex As UriFormatException
                MsgBox(ex.Message)
            Catch ex As WebException
                MsgBox(ex.Message)
    
                'Closes the streamReader if reader isn't anything
            Finally
                If reader IsNot Nothing Then
                    reader.Close()
                End If
            End Try
    
            'Opens form input that puts information into the database
            ImportFile()
    
        End Sub
    
    
    '*******************************************************
    Function DownloadFile(ByVal strfile As String) As Boolean
            Dim responseStream As Stream = Nothing
            Dim fileStream As FileStream = Nothing
            Dim reader As StreamReader = Nothing
            Dim DateToday As Date = Date.Today
            Dim newFile As String = Replace(DateToday, "/", "") & "- Import"
            Dim boolerror As Boolean = False
            Try
                'Creates a webRequest and creates the file that that is called strfile that is declared in the download section
                Dim downloadRequest As FtpWebRequest = WebRequest.Create(strfile)
                'This gets the credentials for the ftpwebrequest @ ftp.dor.in.gov
                downloadRequest.Credentials = New NetworkCredential("########", "#########")
                'This gets the response for the download
                Dim downloadResponse As FtpWebResponse = downloadRequest.GetResponse()
                responseStream = downloadResponse.GetResponseStream()
    
                'this gets the path where the file is located fromthe ftpwebrequest
                Dim fileName As String = Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)
    
                If fileName.Length = 0 Then
                    reader = New StreamReader(responseStream)
                    TextBox1.Text = reader.ReadToEnd()
                Else
    
                    'This is where the one file is created and where it is stored
                    fileStream = File.Create("\\wfspv01\NWC1MCSD\APPS\Apps\MCSD Gross Tax Warrants\dataImport\Processed\import.txt")
                    Dim buffer(1024) As Byte
                    Dim bytesRead As Integer
    
                    'you  have to have this because this actually reads everything that is in the file and puts into the file that is created above
                    While True
                        bytesRead = responseStream.Read(buffer, 0, buffer.Length)
                        If bytesRead = 0 Then
                            Exit While
                        End If
                        fileStream.Write(buffer, 0, bytesRead)
                    End While
                End If
    
                'Writes error messages if they exist
            Catch ex As UriFormatException
                MsgBox(ex.Message)
                boolerror = True
    
            Catch ex As WebException
                MsgBox(ex.Message)
                boolerror = True
    
            Catch ex As IOException
                MsgBox(ex.Message)
                boolerror = True
                'Closes the streamreader, responsestream, and filestream
            Finally
                If reader IsNot Nothing Then
                    reader.Close()
                ElseIf responseStream IsNot Nothing Then
                    responseStream.Close()
                End If
                If fileStream IsNot Nothing Then
                    fileStream.Close()
                End If
            End Try
    
            If boolerror Then
                Return False
            Else
                Return True
            End If
    
        End Function

    It goes through all of that and it's returning files from the past 3 days but no files for today. Today's the 21st of Septemeber and I get files for the 18th, 19th, and 20th from the ftp site and I'm not getting today's file. I even checked went to the site and saw the file dated for the 21st of Septemeber. Can anyone please help me out? I need to get the latest file from the ftp site and I can't get it.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    20

    Re: FTPWebRequest not returning right files

    I fixed it. Even though I had this commented out I eventually reenabled it.

    Dim policy As New Cache.RequestCachePolicy(Cache.RequestCacheLevel.BypassCache)
    listRequest.CachePolicy = policy

    All I did to fix it was replace BypassCache with NoCacheNoStore and now I receive files for today's date and not from dates in the past.

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