禁用 vbs 中的错误 [英] Disable error in vbs

查看:25
本文介绍了禁用 vbs 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码下载链接,但如果 objXMLHTTP.Status 不是 200 然后脚本显示无法下载或找不到链接的错误 &...
如何添加命令,如果 objXMLHTTP.Status 不是 200 ,脚本不显示任何错误?

i use this code for download a link , but if objXMLHTTP.Status not 200 then script show error that can't download or not found link &...
How can add command that if objXMLHTTP.Status not 200 , script don't show any error?

function download(sFileURL, sLocation)

'create xmlhttp object
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

'get the remote file
objXMLHTTP.open "GET", sFileURL, false

'send the request
objXMLHTTP.send()

'wait until the data has downloaded successfully
do until objXMLHTTP.Status = 200 :  wcript.sleep(1000) :  loop

'if the data has downloaded sucessfully
If objXMLHTTP.Status = 200 Then

        'create binary stream object
    Set objADOStream = CreateObject("ADODB.Stream")
    objADOStream.Open

        'adTypeBinary
    objADOStream.Type = 1
    objADOStream.Write objXMLHTTP.ResponseBody

        'Set the stream position to the start
    objADOStream.Position = 0    

        'create file system object to allow the script to check for an existing file
        Set objFSO = Createobject("Scripting.FileSystemObject")

        'check if the file exists, if it exists then delete it
    If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation

        'destroy file system object
    Set objFSO = Nothing

        'save the ado stream to a file
    objADOStream.SaveToFile sLocation

        'close the ado stream
    objADOStream.Close

    'destroy the ado stream object
    Set objADOStream = Nothing

'end object downloaded successfully
End if

'destroy xml http object
Set objXMLHTTP = Nothing

结束函数

下载http://remote-location-of-file"、C:\name-of-file-and-extension"

download "http://remote-location-of-file", "C:\name-of-file-and-extension"

推荐答案

请参阅我关于代码中错误的其他答案.这是一个使用 overwrite 参数的更简洁的版本,因此不需要检查 fso.

See my other answer about the errors in your code. This is a more concise version using the overwrite parameter so no check with fso needed.

function download2(url, destination)
  download2 = false
  on error resume next
  set xml = CreateObject("Microsoft.XMLHTTP")
  xml.Open "GET", url, False
  xml.Send
  if err.number = 0 then
    if xml.readystate = 4 then
      if xml.status = 200 then 
        set oStream = createobject("Adodb.Stream")
        const adTypeBinary = 1,  adSaveCreateOverWrite = 2, adSaveCreateNotExist = 1 
        oStream.type = adTypeBinary
        oStream.open
        oStream.write xml.responseBody
        oStream.saveToFile destination, adSaveCreateOverWrite
        oStream.close
        set oStream = nothing
        download2 = true
      end if
    end if
  end if
  set xml = Nothing
end function

if download2("http://www.textpad.com/download/v60/txpeng600.zig", "txpeng600.zip") then
  wscript.echo "download ok"
else
  wscript.echo "download nok"
end if

'download nok

这篇关于禁用 vbs 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆