' Standard housekeeping Dim i, objFile, objFSO, objHTTP, strFile, strMsg, htmlpage, log, strtime, strdate, strdest, objlog, match, regex, strreg, nomefile, newname Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0 Const FINALDESTINATION="" Const ARCHIVE="" Const MYURL="" Const LOGFOLDER="" Const PREFISSO="" Set objFSO = CreateObject( "Scripting.FileSystemObject" ) strtime=Time strtime=Replace(strtime, ".", "-") strtime=Replace(strtime, ":", "-") strdate=Replace(Date, "/", "-") strtime=Replace(strtime, " ", "-") Set objlog = objFSO.OpenTextFile(LOGFOLDER & strdate & "_" & strtime & ".log",ForWriting, True ) sUsername = "" sPassword = "" htmlpage="" Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" ) 'scarica l'index autogenerato e caricalo nella stringa htmlpage objHTTP.Open "GET", MYURL, False objHTTP.SetCredentials sUsername, sPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER objHTTP.Send if not(objHTTP.statusText="OK") then objlog.write(Time & vbtab & objHTTP.statusText & vbtab & "FILE Non scaricato: indice di " & MYURL & "non raggiungibile") WScript.Quit 1 end if objlog.write(Time & vbtab & objHTTP.statusText & vbtab & "Indice raggiungibile" & vbcrlf) For i = 1 To LenB( objHTTP.ResponseBody ) htmlpage=htmlpage & Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) ) Next 'estrai il nome del file da scaricare e crea l'url completo Set regex = New RegExp strreg = "href\s*=\s*""([^""]+.*prova\.txt)[$""]" regex.Pattern =strreg regex.IgnoreCase = True Set match = regex.Execute(htmlpage) If match.Item(0).SubMatches.Count > 0 Then nomefile=match.Item(0).SubMatches.Item(0) newurl=MYURL+nomefile End If ' Create or open the target file if not(objFSO.fileExists(ARCHIVE+nomefile)) then Set objFile = objFSO.OpenTextFile(ARCHIVE+nomefile,ForWriting, True ) ' Write the downloaded byte stream to the target file objHTTP.Open "GET", newurl, False objHTTP.SetCredentials sUsername, sPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER objHTTP.Send For i = 1 To LenB( objHTTP.ResponseBody ) objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) ) Next objFile.Close( ) if not(objHTTP.statusText="OK") then objlog.write(Time & vbtab & objHTTP.statusText & vbtab & " FILE Non scaricato") WScript.Quit 1 end if objlog.write(Time & vbtab & objHTTP.statusText & vbtab & nomefile & " scaricato" & vbcrlf) 'copia il file scaricato nella cartella di destinazione finale if not(objFSO.FolderExists(FINALDESTINATION)) then objlog.write(Time & vbtab & FINALDESTINATION & " Irraggiungibile") WScript.Quit 1 end if set objfile= objFSO.GetFile(ARCHIVE+nomefile) newname=FINALDESTINATION+PREFISSO+ Left(nomefile,8) + ".txt" objfile.copy(newname) objlog.write(Time & vbtab & newname & vbtab & "Copiato") else objlog.write(Time & vbtab & nomefile & " già scaricato") end if objlog.close()