Download multiple files from an FTP using VBScript

2019-09-17 10:28发布

问题:

I'm not at all an expert at VB Scripting, but since it's a requirement at one of my projects for the moment, I am trying to write a VBScript that will GET all files from a specified FTP Folder.

I manage to get a single specified file, but I can't seem to get all files in a folder. Here's the script I'm trying to use:

    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

It doesn't give me an error or anything, it basically does nothing (and yes, I'm sure there are files in the /foldertocopy from :-))

Any ideas? Something obvious I am missing?

Thanks!

回答1:

I tried your solution but had to make a couple small fixes to make it work:

  • Added Option Explicit (for better detection of undefined variables)
  • Removed the TristateFalse parameter
  • Removed prompt since toggle since interactive mode is already off
  • Changed bye to quit
  • Added -i parameter to the FTP command
  • Plus I tested your FTP command on the command line before using it in your script

Here's the modified script

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


回答2:

I wrote a function to do this for you. You can read and examine it's code here:

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



回答3:

Please find below code to download from ftp location.

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

Note e.g.

sSite : 192.168.0.1



标签: vbscript ftp