2014-09-05 78 views
0

我已經寫了一個用於文件下載的VBScript腳本。我用這條線來構建下載網址:爲什麼我的VBScript字符串連接不起作用?

strURL = DownloadDest & pdfs(n) 

但是當我運行該腳本,strURL只是取DownloadDest值而不pdfs(n)。爲什麼字符串連接不工作?

完整的腳本:

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 
+0

我冒昧地改寫你的問題,使問題描述清楚其他讀者。希望你不介意。 – Helen 2014-09-05 11:09:16

回答

2

的VBScript數組索引從0開始dim pdfs(9)創建了10(不9)元素的數組,但因此它Empty默認情況下,不指定第0個元素。這就是爲什麼在第一次迭代pdf(n)Empty而不是包含文件路徑。

您需要更改您的代碼:

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" 

或者不使用硬編碼索引:

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" _ 
) 

其他提示:

  1. 如果保存文件到您的桌面(而不是其他用戶),請不要硬編碼桌面文件夾路徑。使用SpecialFolders得到它:

    Dim oShell, strDesktop 
    oShell = CreateObject("WScript.Shell") 
    strDesktop = oShell.SpecialFolders("Desktop") 
    ... 
    localfile2 = strDesktop & pdfs(n) 
    
  2. strURLlocalfile2變量僅用於內部getit功能,因此它能夠更好地Dim該功能。

  3. scriptVerDownloadType常量沒有使用,可以刪除。

0

您使用for each通過pdfs迭代:

For Each n In pdfs 
    getit(n) 
Next 

所以npdfs陣列一個字符串,但裏面getit您使用n作爲數組索引:

strURL = DownloadDest & pdfs(n) 

這是一個類型不匹配錯誤。您for each已經提取該數組中的字符串,所以你只需要使用它像這裏面getit

strURL = DownloadDest & n 
相關問題