2012-08-04 79 views
1

代碼從服務器下載CSV文件,然後清理並輸出必要的數據到另一個CSV文件。處理日期時的錯誤處理

我的問題是,在星期日和節假日沒有在服務器上創建的文件。因此,當From和To date之間遇到這樣的日期時,程序將停止進一步。這怎麼可以避免?

如果前一天沒有文件,我希望程序轉到下一個日期。

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

你不看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文檔中的備註部分。

+0

「如果你的文件變得足夠大,它可能仍會在你自己的代碼執行時下載,這意味着文件訪問會失敗,或者更糟糕的是會破壞你的數據。」 – 2012-08-04 12:43:28

+0

那麼,看看MSDN文檔。他們建議您實施通知界面以確定正在下載多少文件。你沒有做到這一點。嘗試用更大的文件測試您的代碼,看看它是否正常。如果不是,你將不得不處理這種情況。請回報您的觀察結果。 – 2012-08-04 15:10:09

+0

您能否通過投票向我表示感謝?或者讓這個批准的答案? :-) – 2012-08-06 07:32:15