2012-04-25 55 views
0

我有一個VB腳本下載從URL文件 - URL是FTP站點:ftp://ftp.zois.co.uk/pub/jcp/Visual Basic腳本下載CSV文件(CSV是空的 - 0字節)

我需要下載指定的文件下面。腳本獲取文件,但內容爲空 - 嘗試使用ftp站點上的其他CSV文件以及相同的問題。

任何人都可以幫忙嗎?

HTTPDownload "ftp://ftp.zois.co.uk/pub/jcp/JCP-scrape-2012-04-24.csv", "C:\" 

Sub HTTPDownload(myURL, myPath) 
' This Sub downloads the FILE specified in myURL to the path specified in myPath. 
' 
' myURL must always end with a file name 
' myPath may be a directory or a file name; in either case the directory must exist 
' 
' Written by Rob van der Woude 
' http://www.robvanderwoude.com 
' 
    ' Based on a script found on the Thai Visa forum 
' http://www.thaivisa.com/forum/index.php?showtopic=21832 

' Standard housekeeping 
Dim i, objFile, objFSO, objHTTP, strFile, strMsg 
Const ForReading = 1, ForWriting = 2, ForAppending = 8 

' Create a File System Object 
Set objFSO = CreateObject("Scripting.FileSystemObject") 

' Check if the specified target file or folder exists, 
' and build the fully qualified path of the target file 
If objFSO.FolderExists(myPath) Then 
    strFile = objFSO.BuildPath(myPath, Mid(myURL, InStrRev(myURL, "/") + 1)) 
ElseIf objFSO.FolderExists(Left(myPath, InStrRev(myPath, "\") - 1)) Then 
    strFile = myPath 
Else 
    WScript.Echo "ERROR: Target folder not found." 
    Exit Sub 
End If 

' Create or open the target file 
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True) 

' Create an HTTP object 
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") 

' Download the specified URL 
objHTTP.Open "GET", myURL, False 
objHTTP.Send 

' Write the downloaded byte stream to the target file 
For i = 1 To LenB(objHTTP.ResponseBody) 
    objFile.Write Chr(AscB(MidB(objHTTP.ResponseBody, i, 1))) 
Next 

' Close the target file 
objFile.Close() 
End Sub 

回答

0

該代碼用於從HTTP下載。 FTP不是HTTP。 FTP確實有一個get命令,但如果沒有別的端口幾乎肯定是不同的,並且由於沒有指定端口,所以幾乎肯定使用80用於HTTP,而FTP使用21作爲其默認值。 Here是一個使用ftp的類似例子。可能還有其他/更好的FTP組件,但它是你問題的根源。另外請注意,它並沒有真正獲取您的文件,它只是創建一個永遠不會寫入的文件。

+0

謝謝 - 有一個ftp程序來同步它 – Dom 2012-04-26 15:43:33