I have no formal background in scripting, so I apologize upfront for my ignorance. I’m trying to find a way download csv files from the internet to a file location using vbscript. After referencing the web, I am able to do this successfully in one case using arguments for the URL and new file location/name. However, I would like to string this together into a for loop to repeat many times, so adding arguments to the command line doesn’t seem like it will work. I realize variables cannot have periods. Is there a way to pull a comma separated arguments w/o having to paste it into the command line? Or alternative, is there a way to package variables into the for loop if they have periods (such as web pages do)?
this one seems to work if I add a single website and file location as the two arguments in command line:
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.Write BinaryGetURL(Wscript.Arguments(0))
BinaryStream.SaveToFile Wscript.Arguments(1), adSaveCreateOverWrite
Function BinaryGetURL(URL)
Dim Http
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
Http.Open "GET", URL, False
Http.Send
BinaryGetURL = Http.ResponseBody
End Function
I cant get this one to work when I try to build the parameters based on variable arrays. Seems to fail at “function” w/ syntax error (periods in variables):
myWebSite = split(commaseparateURLs,",")
myFileLocation = split (commaseparateFiles,",")
num_WebSite = UBound(myFileLocation)
for i = 0 to num_WebSite
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.Write BinaryGetURL(myWebSite(i))
BinaryStream.SaveToFile myFileLocation(i), adSaveCreateOverWrite
Function BinaryGetURL(URL)
Dim Http
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
Http.Open "GET", URL, False
Http.Send
BinaryGetURL = Http.ResponseBody
End Function
Next
Any little nudge on this one will help. Thanks!
You have the
Functiondefinition forGetBinaryFilein the middle of the for loop. Functions must be the root of the file, not inside other code.I’ve also formatted your code by indenting the code in blocks and adding white space. These makes it much more clear what code is inside a Function and which code is inside the for loop.