Reputation: 1
I've written a VBScript script for file download. I use this line to build the download URL:
strURL = DownloadDest & pdfs(n)
But when I run the script, strURL
gets just the DownloadDest
value without pdfs(n)
. Why doesn't string concatenation work?
Complete script:
dim pdfs(9)
pdfs(1) = "Karusel_PF_Promo18_food.pdf"
pdfs(2) = "Karusel_ZCHF_promo18_food.pdf"
pdfs(3) = "Karusel_YF_promo18_food.pdf"
pdfs(4) = "karusel_Moscow_promo18_food.pdf"
pdfs(5) = "Karusel_SVF_promo18_food.pdf"
pdfs(6) = "Karusel_VVF_Promo18_food.pdf"
pdfs(7) = "Karusel_SZF_Promo18_food.pdf"
pdfs(8) = "Karusel_SOCHI_promo18_food.pdf"
pdfs(9) = "Karusel_VLGRD_promo18_food.pdf"
Const scriptVer = "1.0"
const DownloadDest = "http://karusel.ru/manager/img/PDF/"
Const LocalFile = "C:\Users\syurchen\Desktop\"
Const DownloadType = "binary"
dim strURL
dim localfile2
function getit(n)
dim xmlhttp
set xmlhttp = createobject("MSXML2.XMLHTTP.3.0")
strURL = DownloadDest & pdfs(n)
localfile2 = LocalFile & pdfs(n)
msgbox "Download-URL: " & strURL
xmlhttp.Open "GET", strURL, false
xmlhttp.Send
Wscript.Echo "Download-Status: " & xmlhttp.Status & " " & xmlhttp.statusText
If xmlhttp.Status = 200 Then
Dim objStream
set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.Write xmlhttp.responseBody
objStream.SaveToFile localFile2
objStream.Close
set objStream = Nothing
End If
set xmlhttp = Nothing
End function
For Each n In pdfs
getit(n)
Next
Upvotes: 0
Views: 266
Reputation: 1418
you use a for each
to iterate through pdfs
:
For Each n In pdfs
getit(n)
Next
So n
is a string from the pdfs
array, but inside getit
you use n
as an array index:
strURL = DownloadDest & pdfs(n)
That's a type mismatch error. Your for each
has already extracted the string from the array so you just need to use it like this inside getit
:
strURL = DownloadDest & n
Upvotes: 0
Reputation: 98052
VBScript array indexes start from 0. dim pdfs(9)
creates an array with 10 (not 9) elements, but you don't specify the 0th element so it's Empty
by default. That's why on the first iteration pdf(n)
is Empty
instead of containing the file path.
You need to change your code to:
dim pdfs(8)
pdfs(0) = "Karusel_PF_Promo18_food.pdf"
pdfs(1) = "Karusel_ZCHF_promo18_food.pdf"
pdfs(2) = "Karusel_YF_promo18_food.pdf"
pdfs(3) = "karusel_Moscow_promo18_food.pdf"
pdfs(4) = "Karusel_SVF_promo18_food.pdf"
pdfs(5) = "Karusel_VVF_Promo18_food.pdf"
pdfs(6) = "Karusel_SZF_Promo18_food.pdf"
pdfs(7) = "Karusel_SOCHI_promo18_food.pdf"
pdfs(8) = "Karusel_VLGRD_promo18_food.pdf"
Or don't use hard-coded indexes:
Dim pdfs
pdfs = Array ( _
"Karusel_PF_Promo18_food.pdf", _
"Karusel_ZCHF_promo18_food.pdf", _
"Karusel_YF_promo18_food.pdf", _
"karusel_Moscow_promo18_food.pdf", _
"Karusel_SVF_promo18_food.pdf", _
"Karusel_VVF_Promo18_food.pdf", _
"Karusel_SZF_Promo18_food.pdf", _
"Karusel_SOCHI_promo18_food.pdf", _
"Karusel_VLGRD_promo18_food.pdf" _
)
Other tips:
If you save the file to your Desktop (not another user's), don't hard-code the Desktop folder path. Use SpecialFolders
to get it:
Dim oShell, strDesktop
oShell = CreateObject("WScript.Shell")
strDesktop = oShell.SpecialFolders("Desktop")
...
localfile2 = strDesktop & pdfs(n)
The strURL
and localfile2
variables are used only inside the getit
function, so it's better to Dim
them in that function.
The scriptVer
and DownloadType
constants aren't used and can be removed.
Upvotes: 2