Search code examples
batch-filecommand-linesftpwinscp

How to execute WinSCP commands from a batch file


I wanna know if there is a way to run WinSCP commands like open sftp..., from a batch file, without having to enter commands after WinSCP opens up.

Here's what I wanna happen:

  1. I create a batch file (say demo.bat) that opens WinSCP command-line.
  2. I insert WinSCP commands (open, get, put, etc.) in demo.bat.
  3. The batch file demo.bat opens up WinSCP command-line and executes the commands (get, put, etc.) automatically.

Solution

  • Use /command or /script command-line switches.

    There's a guide to automating file transfers to SFTP server with WinSCP.

    A simple batch file using /command switch is like:

    WinSCP.com ^
      /log="C:\writable\path\to\log\WinSCP.log" /ini=nul ^
      /command ^
        "open sftp://username:password@example.com/" ^
        "put C:\local\path\file.txt /remote/path/" ^
        "exit"
    

    And WinSCP GUI can actually even generate the batch file for you:

    enter image description here