使用VBA运行WinSCP脚本 [英] Using VBA to run WinSCP script

查看:391
本文介绍了使用VBA运行WinSCP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to download files from SFTP in CMD window, by using following code:

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

我在线搜索,发现我可以把这些代码放在 文件并使用

I searched online and found out that I can put these codes in a bat file and use

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

但是,只有第一行正在执行bat文件 WinSCP.com 。它会弹出 cmd 窗口,显示此,而不做任何其他事情。

However, only the first line of the bat file WinSCP.com is being executed. It will pop up the cmd window, showing this, without doing anything else.

如何一次执行所有行?

感谢

推荐答案

您拥有的代码不是Windows批处理文件。它是一个Windows命令,后跟WinSCP命令。第一个命令运行 winscp.com 应用程序,然后等待并等待输入。如果最终关闭它,Windows命令解释器( cmd.exe )将继续执行剩余的命令,失败最多,因为它们不是Windows命令。另请参见 WinSCP脚本未在批处理文件中执行

The code you have is not a Windows batch file. It's one Windows command followed by WinSCP commands. The first command runs winscp.com application, which then sits and waits for input. If you eventually close it, Windows command interpreter (cmd.exe) will carry on executing the remaining commands, failing most, as they are not Windows commands. See also WinSCP script not executing in batch file.

因此您必须将命令(打开退出)保存到WinSCP脚本文件(例如 script.txt ),然后使用 / script switch

So you either have to save the commands (open to exit) to a WinSCP script file (say script.txt) and execute the script using the /script switch:

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

或者,使用 / command

来指定WinSCP命令行上的所有命令/ code>切换

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

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

Regarding the quotes: With the /command switch, you have to enclose each command to double-quotes. In VBA string, to use a double-quote, you have to escape it by doubling it.

还要注意,您通常应该使用 / ini = nul 切换隔离从WinSCP配置运行的WinSCP脚本。这样,您还可以确保脚本将在其他计算机上运行。您的脚本不会,因为它缺少 -hostkey switch 更改为验证SSH主机密钥指纹。使用 / ini = nul 将帮助您意识到这一点。

Also note that you generally should use the /ini=nul switch to isolate the WinSCP script run from your WinSCP configuration. This way you can also make sure that the script will run on other machines. Your script won't, as it lacks the -hostkey switch to verify the SSH host key fingerprint. Using the /ini=nul will help you realize that.

您可以拥有 WinSCP GUI生成完整的命令行(包括 -hostkey

另请参阅将文件自动传输到SFTP服务器与WinSCP

这篇关于使用VBA运行WinSCP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆