Code:
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"
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
above code downloads csv files from server and cleans up the files and outputs the necessary data to another csv file.
my issue is on sunday and holidays there is no file created on server. so when such date is encountered in the SELECTION OF FROM AND TO DATE the program stops going further . how can this be avoided
pls help me i want the program to go to next date if there is no file on previous date