0

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
1
  • I took the liberty to reword your question to make the problem description clear to other readers. Hope you don't mind. Commented Sep 5, 2014 at 11:09

2 Answers 2

2

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:

  1. 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)
    
  2. The strURL and localfile2 variables are used only inside the getit function, so it's better to Dim them in that function.

  3. The scriptVer and DownloadType constants aren't used and can be removed.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.