Some time ago I received 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 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 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.exe from 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.

ftprun.cmd

C:\Apps\WinSCP\winscp.com /script=C:\Data\Scripts\ftpscript.txt

ftpscript.txt

option batch continue
option confirm off
open lab-net-01
lcd C:\FTP
synchronize both -delete
synchronize both C:\FTP /
exit

NOTE: In ftpscript.txt file line open lab-net-01 indicated connection profile name which will be used to establish connection.

Before scheduling 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:

C:\>C:\Data\Scripts\ftprun.cmd
C:\>C:\Apps\WinSCP\winscp.com /script=C:\Data\Scripts\ftpscript.txt
batch           continue
confirm         off
Connecting to 172.16.90.41 ...
Connected with 172.16.90.41, negotiating SSL connection...
SSL connection established. Waiting for welcome message...
Connected
Starting the session...
Reading remote directory...
Session started.
Active session: [1] lab-net-01
C:\FTP
Comparing...
Local 'C:\FTP'  Remote '/'
Synchronizing...
Local 'C:\FTP'  Remote '/'
C:\FTP\Test file.txt      |          0 KiB |    0.0 KiB/s | ascii  | 100%
Comparing...
Local 'C:\FTP'  Remote '/'
Synchronizing...
Local 'C:\FTP'  Remote '/'
Test file.txt             |          0 KiB |    0.0 KiB/s | ascii  | 100%
C:\>

Once everything went well we ready to create 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 action list as on the screenshot below:

All parameters have been defined and now w 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 account we want to run this tak under. In example below I just used domain administrator account.

After credentials will be entered in Task Scheduler you can see task added with all parameters defined.

Now it is only matter of time to check if task will run accodingly to schedule we defined and if all files will be transferred correctly.