0

このコードを使用してリンクをダウンロードしますが、objXMLHTTP.Statusが200でない場合、スクリプトはダウンロードできない、またはリンクが見つからないというエラーを表示します&...
objXMLHTTP.Statusが200でない場合、スクリプトが表示されないコマンドを追加するにはどうすればよいですか?エラーはありますか?

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」をダウンロードします

4

2 に答える 2

0

コードのエラーについては、他の回答を参照してください。これはoverwriteパラメータを使用したより簡潔なバージョンであるため、fsoでチェックする必要はありません。

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
于 2012-05-28T18:30:41.670 に答える
0

コードに end 関数がなく、wcript.sleep の行にエラーがあります。簡潔にするためのコメントがない場合は、次のようになります。

function download(sFileURL, sLocation, async)
  set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
  objXMLHTTP.open "GET", sFileURL, async
  on error resume next
  objXMLHTTP.send()
  if err.number = 0 then
    if objXMLHTTP.Status = 200 Then
      set objADOStream = CreateObject("ADODB.Stream")
      objADOStream.Open
      objADOStream.Type = 1
      objADOStream.Write objXMLHTTP.ResponseBody
      objADOStream.Position = 0    
      set objFSO = Createobject("Scripting.FileSystemObject")
      if objFSO.Fileexists(sLocation) then
        objFSO.DeleteFile sLocation
      end if
      set objFSO = Nothing
      objADOStream.SaveToFile sLocation
      objADOStream.Close
      set objADOStream = Nothing
      download = true
    end if
  else
    download = false
  end if
  set objXMLHTTP = Nothing
end function

if download("http://stackoverflow.com/questions/10782976/disable-error-in-vbs", "question.html", false) then
  wscript.echo "download ok"
else
  wscript.echo "download nok"
end if
于 2012-05-28T14:00:08.047 に答える