How to solve file download path error in VBScript ? - 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

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

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

Download Empty File with 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

vbscript to download a file (bypassing invalid certificate errors)

dim xHttp: Set xHttp = createobject("microsoft.xmlhttp")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "https://www.website.com/apps/CertMgr.Exe", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\CertMgr.Exe", 2 '//overwrite
end with
Using the above code I'm trying to download a file from a secure site to install a security certificate automatically, it works fine from a http site, but I'm needing to bypass the security errors. Any ideas?
You need to switch from MSXML2.XMLHTTP to MSXML2.ServerXMLHTTP and use the setOption method with the value SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS. Just place the call between Open and Send. Here's your example updated with the new code.
const SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS = 13056
dim xHttp: Set xHttp = createobject("MSXML2.ServerXMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "https://www.website.com/apps/CertMgr.Exe", False
xHttp.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\CertMgr.Exe", 2 '//overwrite
end with

Resources