使用 VBA 到 运行 WinSCP 脚本
Using VBA to run WinSCP script
我可以使用以下代码从 CMD
window 中的 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
window,显示这个,而不做任何其他事情。
如何一次执行所有行?
谢谢
您的代码不是 Windows 批处理文件。这是一个 Windows 命令,然后是 WinSCP 命令。第一个命令运行 winscp.com
应用程序,然后等待输入。如果你最终关闭它,Windows 命令解释器 (cmd.exe
) 将继续执行剩余的命令,失败最多,因为它们不是 Windows 命令。另见 and WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
因此您必须将命令(open
到 exit
)保存到 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
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。使用 /ini=nul
将帮助您意识到这一点。
你可以有WinSCP GUI generate complete command-line(包括-hostkey
)给你。
我喜欢这个小巧紧凑的程序,并在我自己的项目中使用它。不需要临时文件。快速可靠。
将字符串 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。
我可以使用以下代码从 CMD
window 中的 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
window,显示这个,而不做任何其他事情。
如何一次执行所有行?
谢谢
您的代码不是 Windows 批处理文件。这是一个 Windows 命令,然后是 WinSCP 命令。第一个命令运行 winscp.com
应用程序,然后等待输入。如果你最终关闭它,Windows 命令解释器 (cmd.exe
) 将继续执行剩余的命令,失败最多,因为它们不是 Windows 命令。另见
因此您必须将命令(open
到 exit
)保存到 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
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。使用 /ini=nul
将帮助您意识到这一点。
你可以有WinSCP GUI generate complete command-line(包括-hostkey
)给你。
我喜欢这个小巧紧凑的程序,并在我自己的项目中使用它。不需要临时文件。快速可靠。
将字符串 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。