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
“如果你的文件变得足够大,它可能仍会在你自己的代码执行时下载,这意味着文件访问会失败,或者更糟糕的是会破坏你的数据。” – 2012-08-04 12:43:28
那么,看看MSDN文档。他们建议您实施通知界面以确定正在下载多少文件。你没有做到这一点。尝试用更大的文件测试您的代码,看看它是否正常。如果不是,你将不得不处理这种情况。请回报您的观察结果。 – 2012-08-04 15:10:09
您能否通过投票向我表示感谢?或者让这个批准的答案? :-) – 2012-08-06 07:32:15