2

So far this is the code I'm using to get the All items inside of a folder but inside of this folder, has a sub folder i don't want to include in downloading, is there a way to not include that folder ?

session.GetFiles("/*.*", @"D:\Download\", false, transferOptions);

TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;

TransferOperationResult transferResult;
transferResult =
    session.GetFiles("/*.*", @"D:\Download\", false, transferOptions);

This is the whole code I'm using to download all files

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "sample",
    UserName = "sample",
    Password = "sample",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Your code
    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    TransferOperationResult transferResult;
    transferResult =
        session.GetFiles("/*.*", @"D:\Download\", false, transferOptions);


    // Print results
    foreach (TransferEventArgs transfer in transferResult.Transfers)
    {
        listBox1.Items.Add("Files Downloaded : "+ transfer.FileName);
    }
}

EDIT : This is what I tried in FileMask

TransferOptions transferOptions = new TransferOptions();
transferOptions.FileMask = "/*.*|/.git";


TransferOperationResult transferResult;
transferResult =
    session.GetFiles("/*.*", @"D:\Download\", false, transferOptions);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Bap mop
  • 320
  • 2
  • 15

1 Answers1

4

Use TransferOptions.FileMask

i.e.

TransferOptions transferOptions = new TransferOptions();
transferOptions.FileMask = "*.* | .git/";
...
...
transferResult = session.GetFiles("/someremotefolder/*.*", @"D:\Download\", false, transferOptions);

NOTE: - The pipe | separator combines include and exclude masks.

More details: https://winscp.net/eng/docs/file_mask#include_exclude

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Sash
  • 1,134
  • 11
  • 23
  • Can you check my new code under the EDIT. in the post? I have tried the File mask but it's not working, i have excluded the .git folder but it still getting it. – Bap mop Dec 03 '19 at 02:34
  • 1
    I've just edited my post with a slight mod (note the space between | and a / after .git) AND /someremotefolder - I've removed your original *.* wildcard, as it shouldn't be necessary since you've provided FileMask ... you only need to provide the remote folder and NOT the wildcard options. – Sash Dec 03 '19 at 02:44
  • Is it possible to exclude a folder with a ``Like this Name`` For example I have 2 folder named sample1 and sample2 both of them i want to exclude using this code.``transferOptions.FileMask = "*.*|sample1/";`` – Bap mop Dec 03 '19 at 02:53
  • 2
    It still should be `"/someremotefolder/*"` – on the other hand, there's no need to specify the catch-all include mask in `FileMask` – It can be `transferOptions.FileMask = "| .git/";` (spaces around `|` do not matter) +1 anyway – Martin Prikryl Dec 03 '19 at 08:19