使用Excel VB FtpCommand列出远程目录

时间:2015-09-29 08:02:35

标签: excel excel-vba ftp vba

我需要使用wininet.dll中的FTP命令功能发送一些FTP命令。我使用QUIT命令时它正在工作。但是当我尝试使用例如LSDIR时,我得到了响应0.

Private Declare Function FtpCommand Lib "wininet.dll" Alias "FtpCommandA" (ByVal hConnect As Long, ByVal fExpectResponse As Long, ByVal dwFlags As Long, ByVal lpszCommand As String, ByVal dwContext As Long, phFtpCommand As Long) As Long

Private Function test()
    Dim Success As Long
    Dim iRet As Integer
    Dim lngInet As Long
    Dim lngInetConn As Long
    Dim sCommand As String
    Dim test44 As Long
    sCommand = "DIR"
    Dim test5 As Long
    Dim lError As Long
    Dim strBuffer As String
    Dim lBufferSize As Long
    Dim retVal As Long
    lngInet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)   'Open connection with fpt
    If lngInet = 0 Then
        iRet = MsgBox("bad")
    Else
        lngInetConn = InternetConnect(lngInet, Server.Value, 0, _
            User.Value, Pass.Value, 1, 0, 0)                                 'Connect to server
        If lngInetConn > 0 Then
            Login = True
            blnRC = FtpCommand(lngInetConn, True, FTP_TRANSFER_TYPE_ASCII, sCommand, test44, test5)
            retVal = InternetGetLastResponseInfo(lError, strBuffer, lBufferSize)
        Else
            Login = False
            LoginError
        End If
    End If
    InternetCloseHandle (lngInet)   'Close Ftp I thnik is not necessary
    InternetCloseHandle (lngInetConn)   'Close Connection I thnik is not necessary
End Function

1 个答案:

答案 0 :(得分:1)

  1. FTP中没有DIRLS命令。有LIST命令(或MLSDNLST)。
  2. 无论如何,您无法使用FtpCommand函数检索目录列表。它仅适用于简单的命令。列表(或文件传输)需要与FtpCommand功能未提供的FTP客户端合作。

    改为使用FtpFindFirstFileInternetFindNextFile