Search code examples
vbaexcelwinscp

How to use Excel VBA to run WinSCP Script?


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 Using VBA to run WinSCP script, 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!


Solution

    • 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:[email protected]/"" " + _
        """cd /export/home/Desktop/Temp"" " + _
        """put C:\users\Desktop\JPMC\a.xlsx"" " + _
        """close"" " + _
        """exit""")