1

我们有一项服务,每天提供多个文件供取件。每个文件都附有今天的日期以及文件创建时间的小时、分钟、秒和毫秒标记。

我们的目标是下载给定日期的所有文件,而不考虑时间戳。我设置了以下变量:

Dim remoteFile As String = "/datafeed/sdlookup-total-" & DateTime.Today.Year.ToString 
& "-" & DateTime.Today.Month.ToString("0#") & "-" & DateTime.Today.Day.ToString("0#") & 
".csv.zip"

当我运行控制台应用程序时,我收到一个未找到的 HTTP 550 文件,因为 FTP 上的文件都有当天之后的时间戳,例如

sdlookup-total-2013-07-27_02_15_00_272.csv.zip

模块如下:

Imports System.IO
Imports System.IO.Compression
Imports System.Net
Imports System.Net.WebClient

' This module when run will download the file specified and save it to the local path as defined.
Module Module1
Dim Today As Date = Now()
' Change the value of localFile to the desired local path and filename
 Dim localFile As String = "C:\ForeclosureFile\sdlookoup-total-" & Today.Year.ToString & "-" & 
 Today.Month.ToString("0#") & "-" & Today.Day.ToString("0#") & ".csv.zip"
' Change the value of remoteFile to the desired filename
 Dim remoteFile As String = "/datafeed/sdlookup-total-" & Today.Year.ToString & "-" & 
 Today.Month.ToString("0#") & "-" & Today.Day.ToString("0#") & ".csv.zip"
 Const host As String = "ftp://Datafeed.foreclosure.com"
 Const username As String = "sdlookup"
 Const pw As String = "ourpass"
 Dim strDownLoadTemplate = "sdlookup-total-" & Today.Year.ToString & "-" & Today.Month.ToString
 ("0#") & "-" & Today.Day.ToString("0#") & ".csv.zip"
 Dim strCleanFileForDTS As String
 Dim strLocalZipFile = "C:\ForeclosureFile\ForeclosureFull.zip"
 Dim strLocalCSVFile = "C:\ForeclosureFile\Foreclosurefull.csv"

 Sub Main()
    Dim URI As String = host + remoteFile
    Dim req As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
    req.Credentials = New NetworkCredential(username, pw)

    req.KeepAlive = False
    req.UseBinary = True
    req.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

    Using response As System.Net.FtpWebResponse = CType(req.GetResponse, 
    System.Net.FtpWebResponse)
        Using responseStream As IO.Stream = response.GetResponseStream
            Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
                Dim buffer(2047) As Byte
                Dim read As Integer = 0
                Do
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                Loop Until read = 0
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using
        response.Close()
    End Using
    Dim zipPath As String = "C:\ForeclosureFile\"
    Dim extractPath As String = "C:\ForeclousreFile"

    ZipFile.ExtractToDirectory(zipPath, extractPath)

End Sub

Sub ProcessFile()

    'Downloaded file
    Dim oFile As System.IO.File
    Dim oRead As System.IO.StreamReader
    Dim strLocalCSVFile As String = "C:\ForeclosureFile\sdlookoup-total-" & 
    DateTime.Today.Year.ToString & "-" & DateTime.Today.Month.ToString("0#") & "-" & 
    DateTime.Today.Day.ToString("0#") & ".csv"
    Dim strCleanFileForDTS As String = "C:\ForeclosureFile\ForDTS\sdlookoup-total-" & 
    DateTime.Today.Year.ToString & "-" & DateTime.Today.Month.ToString("0#") & "-" & 
    DateTime.Today.Day.ToString("0#") & ".csv"
    Dim LineIn As String
    'Dim Fields() As String

    'New File
    Dim oNewFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter

    oWrite = File.CreateText(localFile & strCleanFileForDTS)

    oRead = File.OpenText(localFile & strLocalCSVFile)

    '        strLocalCSVFile()

    While oRead.Peek <> -1
        'While oRead.
        LineIn = oRead.ReadLine()
        'Fixes file problem
        oWrite.WriteLine(Replace(LineIn, """", "", 1))
    End While

    oRead.Close()
    oWrite.Close()
End Sub

Sub FTPFileDownload(strtFetchFile As String, PathToSave As String)

    Dim myFtpWebRequest As FtpWebRequest
    Dim myFtpWebResponse As FtpWebResponse
    Dim myStreamWriter As StreamWriter
    Dim strFullPathandFile As String

    strFullPathandFile = PathToSave & strtFetchFile


    myFtpWebRequest = WebRequest.Create("ftp://Datafeed.foreclosure.com/datafeed/" & 
    strtFetchFile)

    myFtpWebRequest.Credentials = New NetworkCredential("sdlookup", "ohpaiH1b")

    myFtpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile
    myFtpWebRequest.UseBinary = True
    myFtpWebRequest.UsePassive = True


    myFtpWebResponse = myFtpWebRequest.GetResponse()

    PathToSave = "D:\test.zip"

    myStreamWriter = New StreamWriter(PathToSave)
    myStreamWriter.Write(New StreamReader(myFtpWebResponse.GetResponseStream()).ReadToEnd)

    myStreamWriter.Close()

    '  litResponse.Text = myFtpWebResponse.StatusDescription

    myFtpWebResponse.Close()


 End Sub

 Public Sub DownloadFiles(ByVal Wildcard As String)
    Wildcard = "sdlookup-total-*.csv.zip"
    Dim Files As String() = GetFiles(Wildcard)
    For Each file As String In Files
        DownloadFile(file)
    Next
 End Sub
End Module

我应该如何修改上述模块,以便在每次执行模块时下载包含 sdlookup-total-"Today'sDate".csv.zip 的所有文件,无论时间戳如何?

4

0 回答 0