0

我在让脚本正常工作时遇到了一些困难。我已经下载了 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
4

2 回答 2

0

为了能够从 Sun Solaris 机器上载/下载文件,我使用了一种名为 Active Experts 的 VBScript。

下载地址: 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
于 2013-08-13T13:50:52.710 回答
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语句/命令

于 2013-08-13T06:32:31.517 回答