使用VBA通过PSCP从SFTP下载/复制文件

时间:2013-08-12 22:37:28

标签: vba download sftp putty

我正在努力让脚本正常工作。我已经下载了PSCP.exe附带的PuTTY软件包,我打算用它来下载/复制SFTP服务器上的文件,然后保存它们或将它们写入我的本地驱动器。

下面是我改为下载的上传脚本的框架。

尝试运行脚本时出现以下错误:

Windows Script Host
    Script: C:\Script.txt
    Line: 2
    Char: 20
    Error: Expected '='
    Code: 800A03F3
    Source: Microsoft VBScript compilation error

有人能指出我正确的方向吗?

提前致谢,

-T

 Sub SftpGet()
    Const cstrSftp As String = """C:\Program Files\PuTTY\pscp.exe"""
    Dim strCommand As String
    Dim pUser As String
    Dim pPass As String
    Dim pHost As String
    Dim pFile As String
    Dim pRemotePath As String

    pUser = "USER" '//User on remote system
    pPass = "PASSWORD" '//User's password on remote system
    pHost = "SFTP.WEBSITE.ORG" '//Ip address of remote system
    pFile = "C:\Important_Info_Copy.txt" '//File to write copy of "pRemotePath" to
    pRemotePath = "/Important_Info.txt" '//Location of file to copy

    strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & pHost & ":" & pRemotePath &_
        " " & pFile &
    Debug.Print strCommand
    Shell strCommand, 1 ' vbNormalFocus '
End Sub

2 个答案:

答案 0 :(得分:0)

1)在你的标题中你说“VBA”,而你似乎把你的代码作为VBScript运行。有不同的语言。

AFAIK,VBScript(与VBA相反)不支持类型变量。删除所有As Type件:

Const cstrSftp = """C:\Program Files\PuTTY\pscp.exe"""
Dim strCommand
Dim pUser
Dim pPass
Dim pHost
Dim pFile
Dim pRemotePath

2)你肯定错过了密码和主机之间的空间。 pPass & pHost应为pPass & " " & pHost

3)远离&装配线

的尾随strCommand

4)另外一些引用会使你的代码更健壮。虽然你目前的输入,它不会导致你的麻烦,因为你没有任何空格。

5)也不确定VBScript是否有Shell语句/命令

答案 1 :(得分:0)

我使用名为Active Experts with VBScript的东西,以便能够从Sun Solaris盒上传/下载文件。

下载可在以下位置找到: http://www.activexperts.com/activsocket/objects/sftp/

下载后,您可以引用“ActiveXperts.SFtp”对象并通过VBScript执行文件传输。下面的示例代码段:

' -----------------------------------------
' Method: doFTPAction()
' Descrip: upload or download a file
' -----------------------------------------
Private Function doFTPAction(p_strAction,_
                             p_strFTPLogFile,_
                             p_strIP,_
                             p_strPort,_
                             p_strUsername,_
                             p_strPassword,_
                             p_strRemoteDirectory,_
                             p_strFileSource,_ 'fully qualified name to source file
                             p_strFileTarget) ' fully qualified name to target file
    Dim blnIsOK
    Dim objSFtp
    Dim strReturnCode

    blnIsOK = False
    Set objSFtp = CreateObject("ActiveXperts.SFtp")

    'setup and connect to the remote server
    objSFtp.Clear  
    objSFtp.Host = p_strIP
    objSFtp.Port = p_strPort
    objSFtp.UserName = p_strUsername
    objSFtp.Password = p_strPassword
    objSFtp.PrivateKeyFile = ""
    objSFtp.LogFile = p_strFTPLogFile
    objSFtp.AcceptHostKey = True
    objSFtp.Connect


    Select Case p_strAction
        Case "DOWNLOAD_FILE"
            strReturnCode = objSFtp.GetFile ("" & p_strFileSource & "", "" & p_strFileTarget & "")
        Case "UPLOAD_FILE"
            strReturnCode = objSFtp.PutFile ("" & p_strFileSource & "", "" & p_strFileTarget & "")
    End Select

    'cleanup
    objSFtp.Disconnect
    Set objSFtp = Nothing

    doFTPAction = blnIsOK
End Function