Option Explicit
Sub Main
   Dim sFileLoc
   Dim nEstrTot,sDataLastEstr,k,r,e
   Dim sDataEstr,nNumEstr,nSalvate
   Dim b
   Dim sFileBd
   Dim sLink
   Dim Ia,N,sVV,x
   Dim CfrData
   sLink = "http://www.lottogam.it/ArchivioTelevideo1945test.txt"
   b = False : N = False
   nSalvate = 0
   sFileBd = GetDirectoryAppData & "BaseDati.dat"
   sFileLoc = GetDirectoryAppData & "temp\"
   If CreaDirectory(sFileLoc) Then
      sFileLoc = sFileLoc & "Estrazioni.txt"
      If DownloadFromWeb(sLink,sFileLoc) Then
         nEstrTot = EstrazioniArchivio
         sDataLastEstr = DataEstrazione(nEstrTot,,,"/")
         Ia = IndiceAnnuale(nEstrTot)
         ReDim aRighe(0)
         Call LeggiRigheFileDiTesto(sFileLoc,aRighe)
         For k = 0 To UBound(aRighe) 'To 0 Step - 1
            ReDim aV(0)
            Call SplitByChar(aRighe(k),"-",aV)
            If Len(aRighe(k)) = 120 Then
               If UBound(aV) = 2 Then
                  sDataEstr = Replace(Left(aRighe(k),10),"-","/")
                  sVV = Right(aRighe(k),110)
                  If b Then
                     If N = False And(Right(sDataEstr,4) <> Right(sDataLastEstr,4)) Then Ia = 0 : N = True
                     If Left(sDataEstr,4) <> Left(sDataLastEstr,4) Then
                        Ia = Ia + 1
                        nNumEstr =(Ia)
                        'ColoreTesto 4 : Scrivi Ia : ColoreTesto 0
                        'ColoreTesto 1 : Scrivi sDataEstr : ColoreTesto 0
                        'ColoreTesto 2 : Scrivi sVV : ColoreTesto 0
                        If nNumEstr > 0 And IsDate(sDataEstr) Then
                           ReDim aEstr(11,5)
                           r = 1 : e = 0
                           For x = 1 To 110 Step 2
                              e = e + 1
                              'Scrivi Mid(sVV,x,2) & " ",0,0
                              aEstr(r,e) = Mid(sVV,x,2)
                              If e = 5 Then r = r + 1 : e = 0 ': Scrivi
                           Next
                           'ScriviMatrice(aEstr)
                           If CfrData <> Left(sDataEstr,5) Then
                              If SalvaEstrazione(aEstr,sDataEstr,nNumEstr,sFileBd) Then
                                 CfrData = Left(sDataEstr,5)
                                 nSalvate = nSalvate + 1
                                 Call Messaggio(nSalvate)
                              End If
                           Else
                           ColoreTesto 2
                           Scrivi "ATTENZIONE " & sDataEstr & " ESTRAZIONE DUPLICATA",True
                           ColoreTesto 0
                           End If
                      
                        End If
                     End If
                  End If
                  If sDataEstr = sDataLastEstr Then b = True
               End If
            End If
         Next
         If nSalvate > 0 Then AllineaArchivi
      Else
         MsgBox "Errore download verificare il link con il browser"
      End If
      Call Scrivi("Sono state aggiunte " & nSalvate & " estrazioni")
      Call Scrivi("")
      Call Scrivi("Estrazioni totali " & QuantitaEstrazioniInFile(sFileBd))
      Call Scrivi("")
      Call Scrivi("Utilizzato archivio del sito " & Left(sLink,23))
      Call Scrivi("")
      'For k = 0 To UBound(aRighe)
      'Call Scrivi(aRighe(k))
      'Next
   End If
End Sub