【问题标题】:Download files from FTP to shared drive将文件从 FTP 下载到共享驱动器
【发布时间】:2017-07-31 12:24:34
【问题描述】:

我有一个 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

【问题讨论】:

  • 您对驱动器有写权限吗?
  • 只需创建一个函数,将文件从驱动器 C: 移动到 G:
  • 你的代码没有使用WinSCP,而是Windows内置的ftp
  • 谢谢大家!不相关的问题,但由于某种原因,我的计算机可以自动运行此代码(它调用 Shell ftp -s...而不需要我打开 cmd),但是当我尝试使用同事时,他不会自动运行 Shell,我必须手动打开cmd运行命令

标签: vba excel ftp


【解决方案1】:

只要改正如下图 它对我有用,希望对你也有用

 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

【讨论】:

  • 您好,请确认上述更正对您是否有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2020-11-17
  • 2019-02-05
  • 2018-07-13
相关资源
最近更新 更多