使用 VBA 运行 WinSCP 脚本 [英] Using VBA to run WinSCP script
问题描述
我可以在 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:UsersxxDesktop
get *.xlsx
# Close and terminate the session
exit
我上网查了一下,发现可以把这些代码放在bat
文件中使用
I searched online and found out that I can put these codes in a bat
file and use
Call Shell("cmd.exe /c C:UsersxxDesktopWinSCPGet.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 脚本未在批处理文件中执行 和 WinSCP 常见问题为什么批处理文件中指定的某些 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 and WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
因此您要么必须将命令(open
到 exit
)保存到 WinSCP 脚本文件(例如 script.txt
)并执行使用 /script
开关的脚本:
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:pathwinscp.com /ini=nul /script=c:pathscript.txt")
或者,在 WinSCP 命令行上指定所有命令,使用 /command代码>开关
:
Alternatively, specify all commands on WinSCP command line, using the /command
switch:
Call Shell("C:pathwinscp.com /ini=nul /command ""open user:pw@address"" ""lcd C:UsersxxDesktop"" ""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
开关 验证 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
) 给你.
You can have WinSCP GUI generate complete command-line (including the -hostkey
) for you.
另请参阅使用 WinSCP 自动将文件传输到 SFTP 服务器.
这篇关于使用 VBA 运行 WinSCP 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!