处理日期时的错误处理

时间:2012-08-04 09:23:09

标签: error-handling vb6

代码从服务器下载CSV文件,然后清理然后向上,并将必要的数据输出到其他CSV文件。

我的问题是,在星期日和节假日,服务器上没有创建文件。因此,当在From和To日期之间遇到这样的日期时,程序将不再继续。如何避免这种情况?

如果上一个日期没有文件,我希望程序转到下一个日期。

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long

Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Private Sub Command1_Click()
On Error GoTo Err
Const q As String = "-"
Dim tmp As String, fName As String, Pos As Long, fPath As String
Dim first As Date, last As Date, spath As String, d As Date
Dim sLineIn As String
Dim sLineOut As String
Dim sCols() As String
Dim sSymbol As String
Dim sName As String
Dim sDate As String
Dim sOpen As String
Dim sHigh As String
Dim sLow As String
Dim sClose As String
Dim sVolume As String
Dim sOpenIntrest As String
Dim sLastSymbol As String
Dim nCounter As String

cap = Me.Caption
If Dir(App.Path & "\NCDEX\", vbDirectory) = "" Then
MkDir App.Path & "\NCDEX\"
End If
spath = App.Path & "\NCDEX\" ' folder to save files : note trailing \
first = MonthView1
last = MonthView2
strURL = "http://www.ncdex.com/Downloads/Bhavcopy_Summary_File/Export_csv/"
For d = first To last
sSourceURL = strURL & Format(d, "MM") & q & Format(d, "dd") & q & Format(d, "yyyy") & ".csv"
Debug.Print sSourceURL
fName = Format(d, "dd-mm-yyyy") & ".csv"
Debug.Print fName
slocalfile = spath & fName
Me.Caption = "Downloading " & fName
Call DeleteUrlCacheEntry(sSourceURL)
URLDownloadToFile 0&, sSourceURL, slocalfile, BINDF_GETNEWESTVERSION, 0&

'  sLastSymbol = "zzz"  'Set to something Symbol cannot possibly be initially.
nCounter = 0
Open App.Path & "\temp.csv" For Output As #2
Open App.Path & "\NCDEX\" & fName For Input As #1
Do While Not EOF(1)
Line Input #1, sLineIn
sLineIn = Replace(sLineIn, Chr(39), vbNullString)
      sLineIn = Replace(sLineIn, Chr(34), vbNullString)
      sLineIn = Replace(sLineIn, " ", vbNullString)
      sCols = Split(sLineIn, ",")
      sSymbol = sCols(0)
      sDate = sCols(15)
      sOpen = sCols(6)
      sHigh = sCols(7)
      sLow = sCols(8)
      sClose = sCols(9)
      sVolume = sCols(10)
      sOpenIntrest = sCols(14)

      If sSymbol = sLastSymbol Then
        nCounter = nCounter + 1
      Else
        nCounter = 1
      End If
      sLastSymbol = sSymbol

      Debug.Print sLineIn
      If sCols(10) <> "0" Then
  ' Only write lines that do not have "0" in Cols(12)

      sLineOut = sSymbol & "_" & nCounter & "," & sDate & "," & sOpen & "," & sHigh & "," & sLow & "," & sClose & "," & sVolume & "," & sOpenIntrest
      Print #2, sLineOut
      End If
    Loop
  Close #1
  Close #2

  ' Delete file 1 and rename file 2 as the original file 1 name.
  ' // Delete the original file
    Kill slocalfile
  ' // Rename the temp file to the original name
    Name App.Path & "\temp.csv" As slocalfile

Next
Me.Caption = cap
'YOU CAN TAKE THIS BELOW OUT IF U DONT WANT IT
MsgBox "Saved to " & spath, vbInformation + vbOKOnly, "Success!"
Exit Sub
Err:     MsgBox "Error", vbCritical + vbOKOnly, "Market Was Closed"
End Sub

1 个答案:

答案 0 :(得分:1)

您没有查看URLDownloadToFile()的返回值。如果文件已开始下载,则返回S_OK(= 0)。如果“指定的资源或回调接口无效”。然后它返回INET_E_DOWNLOAD_FAILURE。

所以你应该这样做:

If URLDownloadToFile(0&, sSourceURL, slocalfile, BINDF_GETNEWESTVERSION, 0&) = S_OK Then

    '  sLastSymbol = "zzz"  'Set to something Symbol cannot possibly be initially.
    nCounter = 0
    Open App.Path & "\temp.csv" For Output As #2
    Open App.Path & "\NCDEX\" & fName For Input As #1
    Do While Not EOF(1)
        Line Input #1, sLineIn
        sLineIn = Replace(sLineIn, Chr(39), vbNullString)
        sLineIn = Replace(sLineIn, Chr(34), vbNullString)
        sLineIn = Replace(sLineIn, " ", vbNullString)
        sCols = Split(sLineIn, ",")
        sSymbol = sCols(0)
        sDate = sCols(15)
        sOpen = sCols(6)
        sHigh = sCols(7)
        sLow = sCols(8)
        sClose = sCols(9)
        sVolume = sCols(10)
        sOpenIntrest = sCols(14)

        If sSymbol = sLastSymbol Then
            nCounter = nCounter + 1
        Else
            nCounter = 1
        End If

        sLastSymbol = sSymbol

        Debug.Print sLineIn
        If sCols(10) <> "0" Then
            ' Only write lines that do not have "0" in Cols(12)
            sLineOut = sSymbol & "_" & nCounter & "," & sDate & "," & sOpen & "," & sHigh & "," & sLow & "," & sClose & "," & sVolume & "," & sOpenIntrest
            Print #2, sLineOut
        End If
    Loop
    Close #1
    Close #2

    ' Delete file 1 and rename file 2 as the original file 1 name.
    ' // Delete the original file
    Kill slocalfile
    ' // Rename the temp file to the original name
    Name App.Path & "\temp.csv" As slocalfile
End If

我还应该提一下,看一下文档,似乎暗示这个函数在文件开始下载时返回。可能是您的文件太小,以至于在函数返回之前已经下载了整个文件。但是,如果你的文件变得足够大,它可能仍然在你自己的代码执行时下载,这意味着文件访问会失败,或者更糟糕的是,会破坏你的数据。

查看MSDN文档中的“备注”部分。