Cool Tool: WinSCP

posted by Yves Lacombe in Best Practices on Feb 26, 2010


I just wanted to share with you a very useful tool that’s been around for a while now, and it helped me solve a problem on a customer’s machine.  To properly investigate, I had to extract several logs from the customer’s server on a scheduled basis and have them FTP’d here so that I could keep tabs on the server’s behavior.

If I were using Linux, I could have used a plethora of command-line tools: wput/wget, rsync and Unison, just to name a few. But I wanted something equivalent for Windows.  I found an open-source implementation of wput/wget, but it was rather glitchy and not really reliable.  Unison exists on Windows, but I find it slow and awkward to use. Lo and behold, I found WinSCP1. I like that it has a clean command-line interface that just does the job reliably.  To me, it’s the “robocopy” of file transfers over FTP/SFTP2.

How to use it

In my case, I just wanted to keep my customer’s Logs folder synchronized with my FTP site on a daily basis.  So all I needed to do was write a small batch file that called WinSCP with a simple script that told WinSCP where to get the files and where to place them, keeping the files in sync. For example,

The dosync.bat file:

  @echo off
  “c:\program files\winscp\winscp.com” /script=sync.txt

Basically, the batch file calls winscp.com, telling it to use the sync.txt script.

The “sync.txt” file:

  open ftp://username:password@ftp.mydomain.com
  synchronize remote d:\log /uploads
  exit

It’s short, sweet and simple:

• The script opens an FTP connection from the source server where WinSCP is installed to my FTP site
• It then uploads all changed files from the source folder (d:\log) to the /uploads folder on the FTP site

I could have done it with a simple ftp script, but WinSCP does both error correction and file completion, which are big pluses.

I now use it all the time.

There is one caveat, though: when you run the installer, it asks if you want to install a third-party application (probably sponsors of the WinSCP project), so you shouldn’t just click “Next,” “Next,” “Next”: pay attention to the install process if you aren’t interested in installing those third-party tools.

References:

1. http://winscp.net
2. http://winscp.net/eng/docs/scripting


Share this article


Share

Comments

Is it possible to sync a folder on the FTP site to a local folder ? I switched the paths in my sync.txt to have ‘syncronize remote /ftpfolder/ftpsubfolder c:\local-folder’ but it doesnt seem to work

By John on 2011 04 19

Hello John, you can’t use the “remote” directive.  The command summary looks like this.

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

So you would use:

synchronize local c:\local-folder /ftpfolder/ftpsubfolder

By Yves Lacombe on 2011 04 20