将文件从FTP下载到共享驱动器

时间:2017-07-31 12:24:34

标签: excel vba excel-vba ftp

我有一个Excel宏可以将文件从WinSCP FTP客户端提取到我的本地C:\users\documents文件夹,但是,当我将文件路径更改为G:驱动器中的文件夹时,shell没有更长时间自动运行我的代码创建的脚本。我的代码创建了脚本,然后我必须手动转到cmd并运行"ftp -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME,以便将文件从FTP传输到G:

到目前为止,这是我的代码,是否有人对我应该做什么有建议?

Option Explicit
Const FTP_ADDRESS = "ip address"
Const FTP_USERID = "user"
Const FTP_PASSWORD = "pw"

Sub GetFile()
    If Not GetFtpFile_F() Then
        MsgBox "Error getting ftp file"
    Else
        MsgBox "Received"
    End If
End Sub


Function GetFtpFile_F() As Boolean
    Dim rc As Integer
    Dim iFreeFile As Integer
    Dim sFTPUserID As String
    Dim sFTPPassWord As String '
    Dim sWorkingDirectory As String
    Dim sFileToGet As String

    Const FTP_BATCH_FILE_NAME = "PULLSCRIPT"

    GetFtpFile_F = False

    sWorkingDirectory = "G:\...\...\folder\"
    sFileToGet = "file." & Format(Now(), "yyyyMMdd")

    On Error GoTo FtpNECAFile_EH

    'Kill FTP process file if it exists
    If Dir(sWorkingDirectory & FTP_BATCH_FILE_NAME) <> "" Then
        Kill sWorkingDirectory & FTP_BATCH_FILE_NAME
    End If

    'Create FTP process file
    iFreeFile = FreeFile
    Open sWorkingDirectory & FTP_BATCH_FILE_NAME For Output As #iFreeFile
    Print #iFreeFile, "open " & FTP_ADDRESS
    Print #iFreeFile, FTP_USERID
    Print #iFreeFile, FTP_PASSWORD
    Print #iFreeFile, "cd " & FTP_USERID
    Print #iFreeFile, "mget " & sFileToGet
    Print #iFreeFile, "y"
    Print #iFreeFile, "quit"
    Close #iFreeFile

    'Shell command the FTP file to the server
    Shell "ftp -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME

    GetFtpFile_F = True

    GoTo FtpNECAFile_EX

FtpNECAFile_EH:
    MsgBox "Err", Err.Name

FtpNECAFile_EX:

    Exit Function
End Function

1 个答案:

答案 0 :(得分:0)

只需进行更正如下图所示 它起作用对我来说希望也适合你

 Print #iFreeFile, "open " & "111.111.1111.111"
   Print #iFreeFile, "c010@abcd.com"
   Print #iFreeFile, "********"
   Print #iFreeFile, "cd " & "/data"
   Print #iFreeFile, "lcd D:\mydata\test"
   Print #iFreeFile, "mget *.csv"
   Print #iFreeFile, "y"
   Print #iFreeFile, "close"
   Print #iFreeFile, "quit"
   Close #iFreeFile
     or 
  iFreeFile = FreeFile
    Open sWorkingDirectory & FTP_BATCH_FILE_NAME For Output As #iFreeFile
    Print #iFreeFile, "open " & FTP_ADDRESS
    Print #iFreeFile, FTP_USERID
    Print #iFreeFile, FTP_PASSWORD
    Print #iFreeFile, "cd " & FTP_USERID

    Dim FTP_mypath As string 
    FTP_mypath ="D:\mydata\test"
    Print #iFreeFile, "lcd " & FTP_Mypath 
    Print #iFreeFile, "mget " & sFileToGet
    Print #iFreeFile, "y"
    Print #iFreeFile, "quit"
    Close #iFreeFile