【问题标题】:Using VBA to run WinSCP script使用 VBA 运行 WinSCP 脚本
【发布时间】:2016-05-17 15:41:03
【问题描述】:

我可以使用以下代码在CMD 窗口中从 SFTP 下载文件:

WinSCP.com
# Connect to the host and login using password
open user:pw@address
# get all the files in the remote directory and download them to a specific local directory
lcd C:\Users\xx\Desktop
get *.xlsx
# Close and terminate the session
exit

我在网上搜了一下,发现可以把这些代码放在一个bat文件中,然后使用

Call Shell("cmd.exe /c C:\Users\xx\Desktop\WinSCPGet.bat", 1)

但是,只有 bat 文件 WinSCP.com 的第一行正在执行。它将弹出cmd 窗口,显示此内容,无需执行任何其他操作。

如何一次执行所有的行?

谢谢

【问题讨论】:

    标签: excel vba cmd winscp


    【解决方案1】:

    您拥有的代码不是 Windows 批处理文件。它是一个 Windows 命令,后跟 WinSCP 命令。第一个命令运行winscp.com 应用程序,然后它坐下来等待输入。如果您最终关闭它,Windows 命令解释器 (cmd.exe) 将继续执行剩余的命令,大多数失败,因为它们不是 Windows 命令。另请参阅WinSCP script not executing in batch file 和 WinSCP 常见问题解答Why are some WinSCP scripting commands specified in a batch file not executed/failing?

    因此,您要么必须将命令(openexit)保存到 WinSCP 脚本文件(例如 script.txt)并使用 /script switch 执行脚本:

    Call Shell("C:\path\winscp.com /ini=nul /script=c:\path\script.txt")
    

    或者,在 WinSCP 命令行上指定所有命令,使用 /command switch:

    Call Shell("C:\path\winscp.com /ini=nul /command ""open user:pw@address"" ""lcd C:\Users\xx\Desktop"" ""get *.xlsx"" ""exit""")
    

    关于引号:使用/command 开关,您必须将每个命令括在双引号中。在 VBA 字符串中,要使用双引号,您必须通过加倍来转义它。


    另外请注意,您通常应该使用/ini=nul switchisolate the WinSCP script run from your WinSCP configuration。这样您还可以确保脚本将在其他机器上运行。您的脚本不会,因为它缺少-hostkey switchverify the SSH host key fingerprint。使用/ini=nul 将帮助您实现这一点。


    您可以拥有WinSCP GUI generate complete command-line(包括-hostkey)。

    另见Automating file transfers to SFTP server with WinSCP

    【讨论】:

      【解决方案2】:

      我喜欢这个小巧紧凑的程序,并在我自己的项目中使用它。不需要临时文件。快速可靠。

      将字符串src(绝对文件路径)解析为uploadImageByFTP。等C:\Users\user\Desktop\image.jpg,就会上传文件。

      替换:

      • <username> 与 FTP 用户
      • <password> 使用 FTP 密码
      • <hostname> 带有 FTP 主机名(等 example.com)
      • <WinSCP.com path> 带有 WinSCP 客户端上的路径(等 C:\Program Files (x86)\WinSCP\WinSCP.com。注意WinSCP.com 而不是 WinSCP.exe
      • <FTP-path> 带有您的 FTP 客户端上的路径(等 /httpdocs/wp-content/uploads)

      -

          Sub uploadImageByFTP(src As String)
              Dim script As Object: Set script = VBA.CreateObject("WScript.Shell")
              Dim waitOnReturn As Boolean: waitOnReturn = True
              Dim windowStyle As Integer: windowStyle = 1
      
              'Not empty
              If (src <> vbNullString) Then
      
                 'Execute script 
                  script.Run _
                      """<WinSCP.com path>"" " + _
                      "/ini=nul " + _
                      "/command " + _
                      """open ftp://<username>:<password>@<hostname>/"" " + _
                      """cd <FTP-path>"" " + _
                      """put " & """""" & src & """""" & """ " + _
                      """close"" " + _
                      """exit""", windowStyle, waitOnReturn
      
              End If
      
          End Sub
      

      -

      WScript.Shell 比默认的Shell() 更强大,因为你可以附加一个waitOnReturn-command;这告诉 VBA,在文件上传到 FTP 服务器之前不允许进一步执行。

      如果您不喜欢在每次执行时打开命令提示符,请将 windowStyle 更改为 0。

      【讨论】:

        猜你喜欢
        • 2021-12-03
        • 2015-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        • 1970-01-01
        • 2023-01-09
        相关资源
        最近更新 更多