1

I have a vbscript that downloads a file. I need to make it so that if there is no internet that it wont pop up with the error message The operation timed out, or Failed to find the resource specified. I've tried using On Error Resume Next, but alas it does not skip any internet related errors. Any way I can set a timeout or something? It is not a large file, just a 20-line text file. Here is my script:

strFileURL = "https://minecraft-statistic.net/en/server/167.114.43.185_25565/json/" 
strHDLocation = "c:\users\public\mc.txt"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
  Set objADOStream = CreateObject("ADODB.Stream")
  objADOStream.Open
  objADOStream.Type = 1 'adTypeBinary
  objADOStream.Write objXMLHTTP.ResponseBody
  objADOStream.Position = 0    'Set the stream position to the start
  Set objFSO = Createobject("Scripting.FileSystemObject")
  If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
  Set objFSO = Nothing
  objADOStream.SaveToFile strHDLocation
  objADOStream.Close
  Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
user692942
  • 16,398
  • 7
  • 76
  • 175
Mark Deven
  • 550
  • 1
  • 9
  • 21
  • 2
    Possible duplicate of [VBScript -- Using error handling](https://stackoverflow.com/questions/157747/vbscript-using-error-handling) – user692942 Sep 28 '17 at 11:15
  • `On Error Resume Next` should work all errors in VBScript are caught by `On Error Resume Next` there is nothing else. Are you sure you have set it right? – user692942 Sep 28 '17 at 11:55
  • It was a repeat error, the script cleared the file, wrote in on Error Resume next, but then cleared it again :/ thanks. – Mark Deven Oct 03 '17 at 20:16
  • And not a duplicate XD I tried those. – Mark Deven Oct 03 '17 at 20:16

1 Answers1

1

On Error Resume Next is the only option for capturing errors, I'm not sure why you say it doesn't work. This works for me;

On Error Resume Next

strFileURL = "https://minecraft-statistic.net/en/server/167.114.43.185_25565/json/" 
strHDLocation = "c:\users\public\mc.txt"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If Err.Number = 0 Then
    If objXMLHTTP.Status = 200 Then
        Set objADOStream = CreateObject("ADODB.Stream")
        objADOStream.Open
        objADOStream.Type = 1 'adTypeBinary
        objADOStream.Write objXMLHTTP.ResponseBody
        objADOStream.Position = 0    'Set the stream position to the start
        Set objFSO = Createobject("Scripting.FileSystemObject")
        If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
        Set objFSO = Nothing
        objADOStream.SaveToFile strHDLocation
        objADOStream.Close
        Set objADOStream = Nothing
    End if
    Set objXMLHTTP = Nothing
Else
    'Handle the error here.
End If

The way On Error Resume Next works is as follows;

  1. Line triggers an error which is caught by the VBScript runtime.
  2. The error is recorded in the Err object.
  3. The line is skipped and the next statement is run.

This process will continue until an On Error Goto 0 line is reached at which point the default behaviour resumes.


Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175