このコードは、サーバーから CSV ファイルをダウンロードし、クリーンアップしてから、必要なデータを別の CSV ファイルに出力します。
私の問題は、日曜日と祝日にはサーバー上にファイルが作成されないことです。そのため、開始日と終了日の間にそのような日付が検出されると、プログラムはそれ以上の処理を停止します。どうすればこれを回避できますか?
前の日付にファイルがない場合、プログラムを次の日付に移動させたい。
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