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 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.
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:
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.
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.
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.
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
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.