0

我没有正式的脚本背景,所以我为我的无知先道歉。我正在尝试找到一种使用 vbscript 将 csv 文件从 Internet 下载到文件位置的方法。引用网络后,我可以在一种情况下使用 URL 和新文件位置/名称的参数成功地做到这一点。但是,我想把它串成一个 for 循环重复很多次,所以在命令行中添加参数似乎不起作用。我意识到变量不能有句点。有没有办法提取逗号分隔的参数而无需将其粘贴到命令行中?或者,如果变量有句点(例如网页),有没有办法将变量打包到 for 循环中?

如果我在命令行中添加一个网站和文件位置作为两个参数,这似乎可以工作:

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

当我尝试基于变量数组构建参数时,我无法让这个工作。似乎在带有语法错误的“函数”中失败(变量中的句点):

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    

对此的任何一点推动都会有所帮助。谢谢!

4

1 回答 1

0

您在 for 循环的中间有 for的Function定义。GetBinaryFile函数必须是文件的根目录,而不是在其他代码中。

我还通过在块中缩进代码并添加空格来格式化您的代码。这使得函数内部的代码以及 for 循环内部的代码更加清晰。

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2

myWebSite = split(commaseparateURLs,",")
myFileLocation = split (commaseparateFiles,",")
num_WebSite = UBound(myFileLocation)

for i = 0 to num_WebSite
    Dim BinaryStream
    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Type = adTypeBinary
    BinaryStream.Open
    BinaryStream.Write BinaryGetURL(myWebSite(i))
    BinaryStream.SaveToFile myFileLocation(i), adSaveCreateOverWrite
Next

Function BinaryGetURL(URL)
    Dim Http
    Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
    Http.Open "GET", URL, False
    Http.Send
    BinaryGetURL = Http.ResponseBody
End Function 
于 2012-12-17T04:48:15.957 回答