A few years ago I received a request to automate file transfer between FTP server and Development systems. Both, FTP server and Development system, are Windows-based. FTP server was running FTP over SSL only, so that automatically eliminated built-in Windows FTP command-line app. As I was using WinSCP in the past, I decided to do quick check if it is possible to use it in batch mode, so I can create script and run it on Windows Task Scheduler to automate whole process.
Prepare environment – application and folders
Here is the folder structure and script files I created on destination machine (to which files were transferred):
- C:\Apps\WinSCP – folder contains binary files of portable WinSCP application
- C:\Data\Scripts – folder contains batch script to run FTP transfer and script with commands for FTP
- C:\FTP – transfer folder to which files will be transferred from remote site
- C:\Data\Scripts\ftprun.cmd – batch file with command which starts WinSCP with certain parameters
- C:\Data\Scripts\ftpscript.txt – text file which contains commands for WinSCP. It is passed to WinSCP as a parameter by ftprun.cmd script
Then download portable version of WinSCP (http://winscp.net/eng/download.php) and unzip to C:\Apps\WinSCP folder.
Create connection profile in WinSCP
First we need to define connection profile in WinSCP. That connection profile will be used later in ftpscript.txt to establish connection with FTP server. In order to define connection profile run winscp.exefrom C:\Apps\WinSCP folder and populate WinSCP Logon window with all details about connection (including password). Once all details are filled in click Save…
In Save session as… window name your session (that name will be used later on in ftpscript.txt to open connection) and make sure that Save password (not recommended) checkbox is ticked. We want to remember password in connection profile (WinSCP stores passwords in encrypted form). Otherwise we will have to entre clear-text password in ftpscript.txt file.
Once connection profile is saved WinSCP will show it on the list in WinSCP Login window. To make sure that all details are correct highlight connection you just created and click Login button. After that WinSCP will attempt to establish connection using parameters just delivered.
If connection is initiated for the first time and FTP server has been defined as FTP with TLS then we can expect that WinSCP might request acceptance for certificate presented by the server. In that case Warning windows will show up with all details of the certificate (as on screenshot below – you can see default, system-generated certificate which has been assigned to FTP site on IIS). Accept certificate by clicking Yes, so WinSCP can store information about certificate in configuration file for future use.
NOTE: It is important to establish connection to FTP server with TLS at least once before transfer task will be sceduled. That way we can accept and save information about certificate, so WinSCP will nit be asking about that later.
Once certificate is accepted and other connection details are fine we should see two panels with files. One panel shows files on our computer, second panel shows files on remote FTP server (in that case there is only one file Text file.txt). That indicates that we can establish connection successfuly and all parameters are entered correctly to connection profile.
As we have connection profile created and saved in WinSCP configuration we can proceed to next step in which we will create some scripts as preparation to automated transfers.
Prepare script to run FTP transfer and script with commands for FTP
In here we will create two scripts which will help to automate file transfer with WinSCP.
- ftprun.cmd – batch file responsible for starting WinSCP command with appropriate parameters
- ftpscript.txt – list of commands for WinSCP to execute
NOTE: Both files will be placed in C:\Data\Scripts folder.
NOTE: In ftpscript.txt file line open lab-net-01 indicated connection profile name which will be used to establish connection.
Before scheduling a script it is worth to check how it works and we get expected results, so from Command Prompt you can just run ftprun.cmd and see how it behaves and if everything is transferred according to the plan.
Here is example output of ftprun.cmd command started before scheduling:
Once everything went well we are ready to create a scheduled task to trigger file transfer automatically. If test connection failed for some reason it needs troubleshooting then.
Creating task in Task Scheduler
In order to schedule file transfer we will use Task Scheduler, which is built-in Windows tool. Task Scheduler is located in Start / All Programs / Accessories / System Tools.
When Task Scheduler window will appear on the screen locate Create Task… in Action panel and click it.
Next on the screen Create Task windows will appear with General tab active by default. In here we can name task (ie. FTP Transfer), put some description which explains what this task suppose to do. Make sure that option Run whether user is logged on or not is chosen. That will allow task to run in background.
Next click on Triggers tab and click New… button. That will allow to create schedule for the task.
You will see New Trigger window on the screen. Indicate in Begin the task that we want to run task On a schedule. Then define schedule for file transfer. On example screenshot schedule indicates to run task every day, every hour indefinitely. Also make sure that task will be Enabled (checkbox at the bottom of New Trigger window).
When you confirm New Trigger with OK button schedule will be added to Triggers list as shown on screenshot below:
Now we can move to Actions tab. On this tab we will define which command should be triggered by task. Click New… to define command.
In New Action window indicate Action as Start a program. Then in Settings section in Program/script textbox enter C:\Data\Scripts\ftprun.cmd (this is batch script which will execute WinSCP with parameters).
Once action is defined click OK button and you should see our command added to the action list as on the screenshot below:
All parameters have been defined and now we can confirm task configuration by clicking OK button. Just after that system will request credentials for that task. It means that we can enter username and password for the account we want to run this task under. In the example below I just used domain administrator account.
After the credentials are entered in Task Scheduler you can see the task added with all the parameters defined.
Now it is only a matter of time to see if the task will run accordingly to the schedule we have defined and if all files will be transferred correctly.