3

I'm rebuilding the FTP portion of a system monitor app, has to be able to download any file that is:

  • .csv
  • .xlsx
  • .xls

and when I try to setup that mask, its returning 0 files, but if I pick just one of them, it works perfectly.

string FileMask = "*.csv; *.xlsx; *.xls";

var sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = ConfigurationManager.AppSettings["FtpHost"],
    UserName = ConfigurationManager.AppSettings["FtpUsr"],
    Password = ConfigurationManager.AppSettings["FtpPwd"],
    SshHostKeyFingerprint = ConfigurationManager.AppSettings["SshHostKeyFingerprint"]
};

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

    List<string> files =
        session.EnumerateRemoteFiles("/", FileMask, EnumerationOptions.AllDirectories)
        .Select(fileInfo => fileInfo.FullName)
        .ToList();

    Console.WriteLine($"Found {files.Count} files");
}

I've tried a couple things and nothing is working the way I want it to.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Eman
  • 1,093
  • 2
  • 26
  • 49

1 Answers1

1

The mask argument of Session.EnumerateRemoteFiles is Windows wildcard. There's no way to match multiple extensions with Windows wildcard.

But you can filter the files yourself. For example using a regular expression:

Regex mask = new Regex(@"\.(csv|xls|xlsx)$", RegexOptions.IgnoreCase);
List <string> files =
    session.EnumerateRemoteFiles("/", null, EnumerationOptions.AllDirectories)
    .Where(fileInfo => mask.Match(fileInfo.Name).Success)
    .Select(fileInfo => fileInfo.FullName)
    .ToList();
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • awesome that's what I needed, is there a way to have it ignore a certain sub folder? I noticed it looks in a sub folder used as a general packrat folder that I'd need to ignore. – Eman Aug 22 '17 at 20:08
  • 1
    The same way. Use `Where` extension method. – Martin Prikryl Aug 23 '17 at 12:32