0

When I'm trying to download an empty file with this script, I get the error: Arguments are of the wrong type, are out of acceptable range or are in conflict with one another. How can I fix it?

Here is my script

    Set objHTTP = CreateObject("WinHTTP.WinHttpRequest.5.1")
    objHTTP.Open "GET", "http://localhost/file.txt", False
    objHTTP.Send

    Dim objStream
    Set objStream = CreateObject("ADODB.Stream")
    With objStream
        .Type = 1
        .Open
        .Write objHTTP.ResponseBody
        .SaveToFile "C:\file.txt"
        .Close
    End With
    Set objStream = Nothing

I have this problem only with empty files.

Michael
  • 2,356
  • 3
  • 21
  • 24
  • "obj.HHTTP.Open" should be "objHTTP.Open" – Ekkehard.Horner Jul 01 '14 at 14:35
  • What line do you get the error on? – SeraM Jul 01 '14 at 14:40
  • Please take a step back and describe the actual problem you're trying to solve instead of what you perceive as the solution. What are you trying to accomplish by downloading an empty file? Do you want to check for existence of that particular file? Something else? – Ansgar Wiechers Jul 01 '14 at 16:34
  • yep, it was typo mistake, I didn't copy paste, because this code is in another network) So my problem was, that I need to download all files, including empty files, but this code doesn't work for empty files. – Michael Jul 01 '14 at 21:56

1 Answers1

2

For an empty file, .ResponseBody is a variant of sub-type Empty. Such a beast can't be written. As you can't create an empty Byte() in VBScript, you have to skip the .Write for an empty file. In code:

Const adSaveCreateNotExist  = 1 ' Default. Creates a new file if the file does not already exist
Const adSaveCreateOverWrite = 2 ' Overwrites the file with the data from the currently open Stream
                                ' object, if the file already exists

Set objHTTP = CreateObject("WinHTTP.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://gent/empty.html", False

objHttp.Send
WScript.Echo objHttp.Status, objHttp.StatusText

Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
    .Type = 1
    .Open
    WScript.Echo TypeName(objHTTP.ResponseBody)
    If Not IsEmpty(objHTTP.ResponseBody) Then .Write objHTTP.ResponseBody
    .SaveToFile "file.txt", adSaveCreateOverWrite
    .Close
End With
Set objStream = Nothing

output:

cscript 24512602.vbs
200 OK
Empty
...
dir
...
01.07.2014  16:54               771 24512602.vbs
01.07.2014  16:54                 0 file.txt
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96