Applications like Filezilla and WinSCP are great simple to use FTP/SFTP clients. However what about if you want to automate uploading files. Imagine you have a brilliant SSIS package, that extracts, manipulates and formats just the right data into the correct format, using SQL Server agent to run your package early in the morning to take advantage of the gap in transactions hitting the database server. You want these extracted data files to be sent to a third party without you needing to log in open WINSCP or Filezilla and manually upload them.
That is what we are going to look at today, a few simple methods for zipping some files and uploading them in an application that can then be set to run on a schedule with something like Windows Task Scheduler.
WINSCP .Net library
WINSCP proveds a .Net library that is a wrapper around the scripting interface available with WINSCP. This library can be used with a SSIS script task or with PowerShell among other things. However we are going to interact with it using C#, mainly because that is what I am used to.
The library has a whole range of classes up uploading and downloading files, helper classes for manipulating directory paths and getting information about the transfer.
How to Zip files
Before we upload the files we need to zip them to redue the size of the transfer. We could do with from SSIS using a Script Task. However it is simple to do in C# as part of our FTP upload application.
Lets start of with an interface
The 2 setter methods are for setting properties from outside the class, passing in the ZipFile name, which could be generated dynamically or set from a config file. The list of files could be from file system lookup of a directory looking for particular file types, or named files.
The CreateArchive method is where the file paths are looped through and added to a Zip Archive, using the name supplied. Once added to the archive each file is deleted.
How to upload the file
Now that we have the files zipped and ready to go we need to add in the WINSCP.Net library.
As we might want to upload different files to different places as part of our automation, lets add the Upload configuration to a simple class.
We can then pass this object which will take data from an ini or config file to our Upload Class.
In the Upload class we have one public class SendFile. This creates the Session Options objects and calls a WINSCP put files operation.
We have used the WINSCP .Net library to create a simple application to automate zipping and uploading of files to an FTP site.