WinSCP is a great tool for accessing SFTP hosts and to retrieve files, it is even better when you can do it on a scheduled basis without having to manually do the job.
Here is how to get files from SFTP automatically using WinSCP!
Pre-requisites
You need to download and install WinSCP
You need to have the following SFTP information
- Hostname, port, username, password, privatekey (optional)
You need to have the following folders in your file system
- c:\temp
- c:\temp\SFTPDownloadedFiles\
This post will show how to create the scripts required to access SFTP with WinSCP, the root folder for the script will be C:\temp
First thing to do is to go to C:\temp and create a file named GetFilesFromSFTP.bat (or whatever you want to call it).
Now enter the following script (assumes you have installed WinSCP in the default install folder)
@echo off
REM Next line NOT needed if you add the WinSCP folder to your Windows
REM PATH environment variable
CD "C:\Program Files (x86)\WinSCP"
Winscp.com /script=C:\temp\GetFilesFromSFTPscript.txt
Next (still in C:\temp) create a file named GetFilesFromSFTPscript.txt - this is the file we are calling in the script above.
Enter the following if only using username and password for authentication against the SFTP host.
option echo off
option batch on
option confirm off
open sftp://username:password@hostname:port
get -delete * C:\temp\SFTPDownloadedFiles\
exit
Enter the following if only using username, password and private key for authentication against the SFTP host. (adding the hostkey is advised and might be required for some SFTP providers)
option echo off
option batch on
option confirm off
open sftp://username:password@hostname:port -privatekey=C:\temp\private.ppk -passphrase=password -hostkey="ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
get -delete * C:\temp\SFTPDownloadedFiles\
exit
If you only want to download a certain type of file, or files with a special naming you can change the get command like so.
get -delete *.pdf C:\temp\SFTPDownloadedFiles\
get -delete *.txt C:\temp\SFTPDownloadedFiles\
get -delete *TextInFile* C:\temp\SFTPDownloadedFiles\
Now you are good to go, just click and run the GetFilesFromSFTP.bat
If you want to automate the process you can make a task scheduler in windows which runs the GetFilesFromSFTP.bat at certain day and time intervals.
Enjoy!