使用VBScript从FTP下载多个文件

时间:2012-02-14 16:02:34

标签: vbscript ftp

我根本不是VB Scripting的专家,但由于目前我的某个项目需要它,我正在尝试编写一个VBScript来获取指定FTP中的所有文件文件夹中。

我设法获得一个指定的文件,但我似乎无法获取文件夹中的所有文件。这是我尝试使用的脚本:

    Dim objOutStream
  Const OpenAsDefault = -2
  Const FailIfNotExist = 0
  Const ForReading = 1
  Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True, TristateFalse)
With objOutStream
    .WriteLine "USER myuser"   ' USERNAME
    .WriteLine "mypass"     ' Password
    .WriteLine "binary"
    .WriteLine "prompt n"
    .WriteLine "lcd /foldertocopyfrom" ' FOLDER I'm changing into
    .WriteLine "mget *"    ' Get all files with today's date in it
    .WriteLine "bye"
    .Close
End With

Set oFTPScriptShell = CreateObject("WScript.Shell")
oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & "C:\temp\temp\empty.txt" & " " & "ftp.location.com", 0, True

它没有给我一个错误或任何东西,它基本上什么都不做(是的,我确定/ foldertocopy中有文件来自: - )

有什么想法吗?我遗失了一些明显的东西?

谢谢!

3 个答案:

答案 0 :(得分:3)

我尝试过您的解决方案,但必须做一些小修补才能使其正常工作:

  • 添加Option Explicit(用于更好地检测未定义的变量)
  • 删除了TristateFalse参数
  • 由于交互模式已经关闭,因此切换后移除了prompt
  • bye更改为quit
  • 在FTP命令中添加-i参数
  • 另外,我在命令行上测试了您的FTP命令,然后在脚本中使用它

这是修改过的脚本

Option Explicit

Const ForWriting = 2

Dim objOutStream, objFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True)
With objOutStream
    .WriteLine "USER myuser"   ' USERNAME
    .WriteLine "mypass"     ' Password
    .WriteLine "binary"
    .WriteLine "lcd /foldertocopyfrom" ' FOLDER I'm changing into
    .WriteLine "mget *"    ' Get all files with today's date in it
    .WriteLine "quit"
    .Close
End With

Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c FTP -n -i -s:" & "C:\temp\temp\empty.txt" & " " & "ftp.location.com", 0, True

答案 1 :(得分:0)

我写了一个函数来为你做这个。你可以在这里阅读和检查它的代码:

http://www.naterice.com/articles/51

答案 2 :(得分:0)

请从ftp位置找到以下代码。

Function FTPDownload(sSite, sUsername, sPassword, sRemotePath)
Const ForWriting = 2
Dim objOutStream, objjFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True)
With objOutStream
    .WriteLine sUsername   ' USERNAME
    .WriteLine sPassword     ' Password
    .WriteLine "binary"
    .WriteLine "cd /"& sRemotePath' FOLDER I'm changing into
    .WriteLine "mget *"    ' Get all files with today's date in it
    .WriteLine "quit"
    .Close
End With
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%Comspec% /c FTP -i -s:" & "C:\temp\temp\empty.txt" & " " & sSite
End Function

注意例如。

sSite:192.168.0.1