Using WinSCP to Automate SFTP Transfers

After using FlashFXP for years I switched to WinSCP as it's simple to add to batch scripts and automate FTP transfers.

Using WinSCP to Automate SFTP Transfers

WinSCP can be called through a bat script in Windows, making it easy to set up a scheduled task to get or push a file through FTP. This allows for example the transfer of csv files from a web server to a local machine for processing into an ERP system.

@echo off

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\WinSCP\Pull.log" /ini=nul ^
  /command ^
    "open sftp://ftpuser:[email protected]/ -hostkey=""ssh-rsa 2048 id_rsa.pub key goes here""" ^
    "lcd ""C:\WinSCP\Exports\""" ^
    "cd /var/www/import/" ^
    "put file.csv" ^
    "exit"
	
del "C:\WinSCP\Exports\file.csv" /q

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

exit /b %WINSCP_RESULT%