tags:

views:

401

answers:

5

FTP doesn't support the notion of transferring entire directories. much less the option to /MIRror them a la Robocopy.

Given a local folder C:\MyFolder and an FTP destination ftp://example.com/home/MyFolder, I'm looking for some solid code to transfer the contents of the local folder.

  • If the destination exists, the contents of the local folder is uploaded there, overwriting existing files if necessary.
  • If the destination does not exist, the destination is created first.

Full mirroring (including deleting) would be nice, but not necessary.

I'm in C# / Windows. Thanks!

Edit: Libraries are fine, but I'd prefer free ones!

+1  A: 

I have done this in the past and found it much easier to use a library. C# does support various network programming APIs including FTP in the System.Net namespace, but handling all the corner cases and errors is a pain.

I can't recal the library I used, but a web search showed a few current O/S projects as well as commercial products.

E.g. http://ftplib.codeplex.com/

Steve Haigh
+1  A: 

Take a look at the Xceed FTP component if you don't mind purchasing/using libraries. With this library you could achieve what you want in about 10-15 lines of code.

The built-in C# FTP support is good for nothing more than grabbing a file over FTP.

http://xceed.com/FTP_NET_Intro.html

Andy Shellam
+2  A: 

WinSCP might be suitable for you. It's available as single, no-install executable (as portable executable), has a command line interface, and allows mirroring.

The Chairman
This is what I used in the end. WinSCP is pretty awesome, and free. The /console switch lets you effectively pass a script as a set of command line arguments, which made it easy to generate a Process.Start call from my C# app with similar semantics to my existing use of Robocopy /MIR. Thanks!
Pete Montgomery
A: 

Following code shows how to upload the whole directory to the server using the commercial FTP component Rebex FTP:

// create client, connect and log in 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");

// upload the content of 'c:\data' directory and all subdirectories 
// to the '/wwwroot' directory at the server 
client.PutFiles(@"c:\data\*", "/wwwroot", 
  FtpBatchTransferOptions.Recursive, 
  FtpActionOnExistingFiles.OverwriteOlder);

client.Disconnect();

It handles unlimited folder hierarchy, symlinks, hardlinks, junction points and similar beasts. Conflicts can be resolved automatically (as shown on code above) or displayed to the user for resolution.

More info

Disclaimer: I'm involved in the development of this product.

Martin Vobr
Martin, this post and [this one](http://stackoverflow.com/questions/1890886/downloading-multiple-files-from-ftp-server/2343689#2343689) are being flagged as spam. I realize your user name has Rebex in it, but not everyone will see it there. Can you please make your connection more clear in these two answers?
Michael Myers
@mmyers: Added disclaimer to both posts.
Martin Vobr
A: 

This script will upload entire directory structure to an FTP server including text, binary etc. files. It creates subdirectories as needed.

http://www.biterscripting.com/SS_FTPUpload.html

Since you were looking for free, this is free. All you have to do is get your entire directory structure ready on the computer, then you execute this command in biterscripting.

script "SS_FTPUpload.txt" ftpserver("ftp.mycompany.com") ftplogin("me") ftppassword("my password") localpath("/mywebsite") remotepath("/pages")

Enter the whole above command on one line. Use correct values in place of "ftp.mycompany.com", "me", "my password", etc.

P M