How to use Excel VBA to run WinSCP Script?

五迷三道 提交于 2019-12-08 03:59:52

问题


I wrote a WinSCP script that upload a local file to a SFTP site. The script is saved into .txt:

WinSCP.com
open sftp://username:password@address
cd /export/home/Desktop/Temp
put C:\Users\Dekstop\JPMC\a.xlsx
close
exit

Then I look at this post and write this code into Excel VBA:

Sub RunWinScp()
Call Shell("C:\Program Files (x86)\WinSCP\WinSCP.com /ini=nul/script=C:\Users\Desktop\WinSCPGetNew.txt")
End Sub

But when I try to run it, nothing happens. There is no error, but the file is not transferred correctly neither. Could someone please help?

Thanks a lot!


回答1:


  • You need to enclose the path to winscp.com with double quotes, as it contains spaces
  • You need a space between the /ini=nul and /script=...
Call Shell("""C:\Program Files (x86)\WinSCP\WinSCP.com"" /ini=nul /script=C:\Users\Desktop\WinSCPGetNew.txt")
  • Remove the winscp.com at the beginning of your WinSCPGetNew.txt script. There's no winscp.com command in WinSCP. You already run WinSCP.com by the Shell function in VBA. This is actually already covered in the question you link to yourself: Using VBA to run WinSCP script.

Though you better specify the commands on WinSCP command-line using the /command switch to avoid a need to for a separate commands file:

Call Shell( _
    """C:\Program Files (x86)\WinSCP\WinSCP.com"" " + _
    "/ini=nul " + _
    "/command " + _
    """open sftp://username:password@example.com/"" " + _
    """cd /export/home/Desktop/Temp"" " + _
    """put C:\users\Desktop\JPMC\a.xlsx"" " + _
    """close"" " + _
    """exit""")


来源:https://stackoverflow.com/questions/40247276/how-to-use-excel-vba-to-run-winscp-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!