Automate File Transfer with WinSCP

In our first WinSCP article we installed WinSCP and explored some of its basic features . In this article we'll explore some advanced features of WinSCP including automated file transfers and synchronization.


WinSCP Executables

WinSCP has two executables: winscp.exe and winscp.com.

winscp.exe is for the gui and winscp.com is for its console. You can run winscp.exe /console in your command prompt which will start a new console (same as running winscp.com or just winscp). You can type winscp /help to learn more about winscp’s arguments.

It would be good idea to add WinSCP executables in the system path for easy access from command prompt:

Go to Start->Control Panel->System->Advanced Settings->Environmental Variables then edit the Path variable by putting ;C:\Program Files\WinSCP at the end of the line.

Path

How to automate file transfers

WinSCP provides a strong set of command line tools, useful for writing scripts. Let's open notepad in Windows and write our first script.

option batch on
option confirm off
open mySlice1
get /home/users/demo/example_file.txt C:\Backups\
exit

save this file as testScript.txt.

Basically, we are telling WinSCP that it can answer all prompts negatively and disable overwrite confirmation.

Then the script opens our mySlice1 session which we configured in the first WinSCP article and connects to our demo slice .

The script then downloads example.txt file from the /home/user/demo directory to the local 'Backups' directory and closes the session.

Don't forget the backslash at the end of your second argument for the get command. Otherwise you will get an error. If you want to save example_file.txt under a different name, then you can simply add the new name of the file to the second argument; C:\Backups\newFileName.

You can run the script with the following command:

 winscp /script=C:\myScripts\testScript.txt 

You can also use a single command to run this script:

winscp /command "option batch on" "option confirm off" "open mySession1" "put examplefile.txt /home/user/demo" "exit" 

You may realize that we used the /command switch to pass our instructions from the command line.

Now, we'll learn how to automate this script in our Windows machine so we can transfer any files or directories to and from the slice. For simplicity we will use the script from above. However, we can transfer as many files as we want.

In our scenario, we will backup example_file.txt (located on the Slice) to our local machine everyday at 8PM.

Windows has a Task Scheduler tool which will help us to automate our script. To access the task scheduler in Windows Vista, go to Control Panel-> Administrative Tools-> Task Scheduler. Now, click Create a Basic Task. We will use backup as the name of the task.

task scheduler

Click next to choose how frequently we want to run the script. In this case, I chose daily. On the next screen set 8pm as the starting time.

task scheduler

Select "start a program" as our task action. In the next screen, we are asked our program name and its arguments. Put winscp in the program name box, and /script =C:\myScripts\testScript.txt in the arguments box. After you click next, it will show the summary of our task.

task scheduler

Nice!

How to Synchronize Directories

Sometimes it will be useful to work in a local directory and then synchronize those files with a directory on our slice. WinSCP make this possible with a single command.

Suppose we want to synchronize our local directory C:\www with our remote directory /home/demo/public_html.

 winscp \command "option batch on" "open mySlice1" "synchronize remote C:\www /home/demo/public_html" "exit" 

Note that the remote directory must be specified after the local directory, otherwise the command doesn't work. For a full explanation of the syntax, please refer to the official documentation

Summary

Quite a lot here, but as you see WinSCP makes our life easier with its scripting capability. By combining it with Windows Task Scheduler, we can automate file transfer between slices and our local machines. Moreover, we can synchronize remote and local directories. WinSCP has lots of commands. Please check here for WinSCP commands.

Ismail

Article Comments:

Carlos Fretes commented Fri May 07 17:37:02 UTC 2010:

Thank's I use Winscp is very very flexible and secure, is a grate post, sorry for my english.

Best Regards

Alex K commented Tue Nov 09 07:42:09 UTC 2010:

Nice one! I was wondering if you could tell me if winscp can upload entire folder structures?

Mainsh bhandari commented Mon Nov 22 13:45:21 UTC 2010:

I've winscp installed on my window 2008 machine when I run the script manually, it runs perfectly but same thing thru Window Scheduler hangs up.

Regards Mainsh Bhandari

jim mcdonnell commented Wed Jun 08 19:22:36 UTC 2011:

Hi I have a simple script where I am trying to put 1 file a day to a remote server. My bat file looks like this:

ECHO Start the process REM CD C:\Program Files\WinSCP
winscp.exe /console /script=abie.txt

open cdbb:T0@password@167.139.123.177:2022 cd In
put HelloWorld1.txt bye

Eddie Michel commented Mon Sep 26 15:35:39 UTC 2011:

We have a problem that the file gets deleted on remote server after downloading it. Is that an option made on the server itsself or is something missing?

We don't have any delete commands in the text file... :((

Raed commented Tue Nov 29 21:24:33 UTC 2011:

Can somebody tell me the code of sending files from a folder to an SFTP server. I need a quck reply please...thanks

TMH commented Mon Jan 30 04:44:03 UTC 2012:

Good Article ! Nice Work Ismail:)

EFC commented Fri Feb 17 20:33:31 UTC 2012:

Using WinSCP thru automated script. Need an example where the script accepts 'Any' SSH Hostkey. Regards

Merciless Mike commented Fri Feb 24 20:23:32 UTC 2012:

I've setup a script to move from a Windows directory to SCP server. However, if there is an open file, then it fails. Does anyone have a way around this issue. ** Any open file is ok to skip, but how do you tell the script to skip all ??

Vijay commented Fri Mar 09 18:31:17 UTC 2012:

Hello Many thanks for the explanation I have a query with regards to the command sychronize

Basically I am trying to fetch files from a Linux server to my windows machine. How can I selectively synchronise my local folder - meaning if I only want specific files from the remote directory that were updated after a specific time? Can this be done with scripts? Thanks

Mihir commented Tue May 29 18:04:30 UTC 2012:

Thanks a lot !!

Nithin commented Wed Oct 24 13:29:49 UTC 2012:

I am trying to upload the documents from the C: drive to a remote server.I wrote a script by looking in to your sample. while trying to execute it always ask for the hostname, user id and the password which I already mentioned while setting up the connection. Below is the details of the test script which i wrote, can you please check and let me know whether it is correct:

option batch on option confirm off open ibsifgmg@fts2.jpmchase.com put C:\test /SA/ exit

Here /SA/ is the remote location where i need to upload some files, is this command fully transfer the files from the C: drive or it keeps a copy in C: drive aswell.

Josh commented Tue Apr 30 14:14:30 UTC 2013:

Excellent article! I was trying to find a clear instruction on using WinSCP in Windows Server 2008 for a full day before I came across your site. Very clear and you got me on the right track! Much appreciated.

Henry commented Wed Aug 20 15:30:11 UTC 2014:

Great article. Better than the documentation. I want to know how I can use the settings in the GUI for transferring files when automating from a script. Thanks.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)