FTP uploading files using patterns

Uploading files using patters is a unique feature of our FTP component. It allows you fast upload of files of certain types.

Here’s the sample that uploads all text files (*.txt) files from C drive, to newly created ‘Uploads’ folder on FTP server. The search includes all child folders recursively – note the true parameter in LocalSearchOptions constructor. Ftp component is going to create all necessary folders on the remote FTP server for you.

Upload using wildcard pattern

// C#

using (Ftp ftp = new Ftp())
{
    ftp.Connect("ftp.example.com");    // or ConnectSSL

    ftp.CreateFolder("Uploads");

    LocalSearchOptions options = new LocalSearchOptions("*.txt", true);
    ftp.UploadFiles("Uploads", @"c:\", options);

    ftp.Close();
}
' VB.NET

Using ftp As New Ftp()
    ftp.Connect("ftp.example.com")	' or ConnectSSL
    ftp.CreateFolder("Uploads")

    Dim options As New LocalSearchOptions("*.txt", True)
    ftp.UploadFiles("Uploads", "c:\", options)

    ftp.Close()
End Using

Upload using regex pattern

You can also use LocalSearchOptions.UseRegexMatch method, if you want to use regex patterns:

// C#

using (Ftp ftp = new Ftp())
{
    ftp.Connect("ftp.example.com");    // or ConnectSSL

    ftp.CreateFolder("Uploads");

    LocalSearchOptions options = new LocalSearchOptions();
    options.UseRegexMatch(@"^.*$", @"^.*\.txt$", true);
    ftp.UploadFiles("Uploads", @"c:\", options);

    ftp.Close();
}
' VB.NET

Using ftp As New Ftp()
    ftp.Connect("ftp.example.com")	' or ConnectSSL

    ftp.CreateFolder("Uploads")

    Dim options As New LocalSearchOptions()
    options.UseRegexMatch("^.*$", "^.*\.txt$", True)
    ftp.UploadFiles("Uploads", "c:\", options)

    ftp.Close()
End Using

Tags: ,

4 Responses to “FTP uploading files using patterns”

  1. Michael Jepson Says:

    How can I use UploadFiles to the root folder on the server?

    I tried:
    client.UploadFiles(“.”, this.LocalFolder, new LocalSearchOptions(“*.jpg”, false));
    and
    client.UploadFiles(“/”, this.LocalFolder, new LocalSearchOptions(“*.jpg”, false));

    But both give an error, stating that the target folder on the server does not exist.

    Regards,
    Michael

  2. Limilabs support Says:

    @Michael

    Have you tried getting the current folder name just after you logged in:

    string folder = client.GetCurrentFolder();
    

    Your account root folder may be not the same as the server folder (e.g. “/ftp/michael/”).

  3. Pawan Says:

    how to upload a single and particular file……?

  4. Limilabs support Says:

    @Pawan,

    You just need to use Upload method. There are several overloads of this method, that allow to uploading byte array, stream, or simply uploading file from disk:
    http://www.limilabs.com/blog/upload-file-using-ftp

Questions?

Consider using our Q&A forum for asking any questions.