2012-04-25 49 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