Commandline FTP folder download

Edit on GitHub

A quick annoyed post... I just spent two hours searching the Internet for a means on how to recursively download a complete FTP folder, command-line, and in a simple way. Oh yeah, and preferably freeware.

The solutions I found were not what I expected: a $50 software product providing a GUI (I said command-line! [:@]), a bloated scheduler thingy that does download in the background (I said simple! [8o|]), to batch-files relying on Windows built-in ftp.exe and a gigantic list of all files that need to be downloaded.

Here's the thing: the searching really p*ssed me off! Not one thing provides the amount of ease I demand! Luckily, my good friend C# came to the rescue. CodeProject.com provided me this article on a ready-to-use FTP client class. Some additional magic, a glass of cola and... Here's FTPFolderDownload version 1.0! Feel free to download, compile, modify, abuse, ... this piece of code.

Usage is simple: pass along some command line arguments (list is below), and see your FTP files coming in.

List of arguments:
        /server="<server hostname>"
        /username="<username>"
        /password="<password>"
        /remoteFolder="<remote folder>"
        /localFolder="<local folder>"
        /recursive

This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.

Leave a Comment

avatar

6 responses

  1. Avatar for Michael
    Michael November 18th, 2007

    Really great thing!

    Only problem I had were the parent directories that were listed by the ftp request but not handled by the prog.
    Changes:

    public enum DirectoryEntryTypes
    {
    File,
    Directory,
    ParentDirectory
    }

    public FTPfileInfo(string line, string path)
    {
    //parse line
    Match m = GetMatchingRegex(line);
    if (m == null)
    {
    //failed
    throw (new ApplicationException("Unable to parse line: " + line));
    }
    else
    {
    _filename = m.Groups["name"].Value;
    _path = path;

    Int64.TryParse(m.Groups["size"].Value, out _size);
    //_size = System.Convert.ToInt32(m.Groups["size"].Value);

    _permission = m.Groups["permission"].Value;
    string _dir = m.Groups["dir"].Value;
    if (_dir != "" && _dir != "-")
    {
    string testForDots = Filename.Replace(".", "");
    if (testForDots.Length == 0)
    _fileType = DirectoryEntryTypes.ParentDirectory;
    else
    _fileType = DirectoryEntryTypes.Directory;

    }
    else
    {
    _fileType = DirectoryEntryTypes.File;
    }

    try
    {
    _fileDateTime = DateTime.Parse(m.Groups["timestamp"].Value);
    }
    catch (Exception)
    {
    _fileDateTime = Convert.ToDateTime(null);
    }

    }
    }

    private void _downloadFTPFolderRecursive(FTPclient client, string remoteFolder, string localFolder, bool recursive)
    {
    // Get list of folders/files and download them
    FTPdirectory remoteFolderInfo = client.ListDirectoryDetail(remoteFolder);
    foreach (FTPfileInfo fileInfo in remoteFolderInfo)
    {
    // Download file
    if (fileInfo.FileType == FTPfileInfo.DirectoryEntryTypes.File)
    {
    Console.WriteLine("Downloading file " + fileInfo.FullName + "...");
    client.Download(fileInfo, localFolder + fileInfo.FullName.Replace(remoteFolder, ""), true);
    Console.WriteLine("Downloaded " + fileInfo.FullName + ".");
    }
    else if (fileInfo.FileType == FTPfileInfo.DirectoryEntryTypes.Directory)
    {
    // Recursive download?
    if (recursive)
    {
    // Folder exists? If not, create it!
    string benoetigterOrdner = localFolder + fileInfo.FullName.Replace(remoteFolder, "");
    if (!Directory.Exists(benoetigterOrdner))
    {
    string newFolder = localFolder + fileInfo.FullName.Replace(remoteFolder, "");
    Directory.CreateDirectory(newFolder);
    }

    // Download folder
    Console.WriteLine("Downloading folder " + fileInfo.FullName + "...");
    _downloadFTPFolderRecursive(client, fileInfo.FullName, localFolder + fileInfo.FullName.Replace(remoteFolder, ""), recursive);
    Console.WriteLine("Downloaded folder " + fileInfo.FullName + ".");
    }
    }
    }
    }

    best wishes
    Michael

  2. Avatar for TrentTompkins
    TrentTompkins February 11th, 2008

    Amazingly hard to find how to do this. This program doesn't work either. It reads . as a directory, so, the first path it reads is:

    public_html/
    then public_html/./
    then public_html/././
    public_html/./././

    ect, ect, ect.

  3. Avatar for maartenba
    maartenba February 11th, 2008

    I updated the downloadable file with all comments.

  4. Avatar for joniba
    joniba April 16th, 2008

    Thanks, this information is very hard to find.

    You rock man

  5. Avatar for MattO
    MattO June 9th, 2008

    Just use Wget, it's been around forever and works fine for doing this and is open source.

  6. Avatar for radeonorama
    radeonorama August 18th, 2008

    Hello, just trying to use this tool as it is just what i need!!! I seem to be getting an exception 'System.ApplicationException' Could anybody help me at all?

    Thanks in advance!