Technical Recipes

How to use WinSCP to synchronize files between local and remote

1. Synchronize timestamps

    Before doing file synchronization, make sure that WinSCP knows correct timestamps of remote files, to avoid losing data.

    If you happen to have the same files locally and remotely with the different timestamps, you can update them without transferring the files again. Use function Synchronize in Synchronize timestamps mode. 

    Choose from the menu on the top: Commands -> Synchronize or click the toolbar icon and choose Synchronize timestamps in the "Mode".

2. Using Directory Synchronization

    2.1 Choose a synchronization direction

	Select one of the Both/Remote/Local as the target directory.

	- Local direction: changes from remote directory (source) are applied to local directory (target). Only the local directory is modified. 

	- Remote mode: the same as Local, just in an opposite direction: changes from the local directory (source) are applied to the remote directory (target). Only the remote directory is modified. 

	- Both mode: both local and remote directories can be modified (both can act as both source and target). 

    2.2 Choose a synchronization mode

	Select one of the Synchronize files/Mirror files/Synchronize timestamps mode.

	- Synchronize files mode: the newer files in source directory are transferred to the opposite directory. Files in source directory not present in target directory can optionally be transferred as well (disable this using the option Existing files only). Files in target directory not present in source directory are optionally deleted (enable option Delete files). In Both mode (see above), file not present in an opposite directory is considered new, hence it is transferred (if allowed), but never deleted. In other words, in Both mode, no file is ever deleted.

	- Mirror files mode: the different (both newer and older) files in source directory are transferred to the opposite directory. Otherwise the mode is the same as Synchronize files. 

	- Synchronize timestamps mode: the timestamps of target files are updated to match timestamps of source files. It will not do any transfers, nor delete anything. Simply, whenever it finds the same file in both directories it updates the timestamp of target file to match the one of source file. In Both mode, it always updates the older timestamp. The mode is available with SFTP protocol only. 

3. Using command line script for automation

    3.1 Add WinSCP program into 'path' system variable

    3.2 Enter the console mode in DOS prompt

    	# winscp

    	> synchronize local|remote|both [ <local directory> [ <remote directory> ] ]

	e.g.,
	
   	> synchronize both
   	> synchronize remote -delete d:\www /home/martin/public_html
   	> synchronize both -filemask="|*.bak; *.tmp"
	
	When the first parameter is local, changes from remote directory are applied to local directory. When the first parameter is remote, changes from the local directory are applied to the remote directory. When the first parameter is both, both local and remote directories can be modified.

    3.3 Create script file to run the synchronization task

    	> winscp.com [/script=script_file] [/command command1 command2 ...] [/parameter param1 ...]

	e.g.,
	
   	> winscp.com /script="C:\Users\martin\Documents\myscript.txt"
   	> winscp.exe /console /script="myscript.txt"
	
	Use /defaults parameter along with /upload, /synchronize or /keepuptodate to skip the settings dialog and start the operation straight away with default settings
          
	It is recommended to escape the arguments with // switch. e.g.,
		
        > winscp.exe /defaults /synchronize // "C:\Users\martin\Documents\MySite" /home/martin/public_html