Regular expressions are not supported in GetFiles/PutFiles methods, but it is a feature we are currently working on. It will be added in new release.
In the mean time you can use following simple implementation of GetFiles method which uses a regular expression to filter files/directories to be transferred.
Please leave a comment if you need a VB.NET code or if you want also the PutFiles method.
static void Main(string[] args)
{
Ftp client = new Ftp();
client.Connect(...);
client.Login(...);
GetFiles(client, "/data", @"c:\temp", new Regex("file[0-2].txt"), new Regex("dir(0|1)"));
client.Disconnect();
}
private static void GetFiles(Ftp client, string remotePath, string localPath, Regex fileNameFilter, Regex directoryNameFilter)
{
List<string> list = GetListRecursive(client, remotePath, fileNameFilter, directoryNameFilter);
foreach (string remote in list)
{
string local = Path.Combine(localPath, GetRelativePath(remote, remotePath));
string localDirectory = Path.GetDirectoryName(local);
if (!Directory.Exists(localDirectory))
Directory.CreateDirectory(localDirectory);
client.GetFile(remote, local);
}
}
private static List<string> GetListRecursive(Ftp client, string remotePath, Regex fileNameFilter, Regex directoryNameFilter)
{
List<string> paths = new List<string>();
client.ChangeDirectory(remotePath);
FtpList list = client.GetList();
foreach (FtpItem item in list)
{
string name = item.Name;
string path = FtpPathCombine(remotePath, name);
switch (item.Type)
{
case FtpItemType.Directory:
if (directoryNameFilter.IsMatch(name))
paths.AddRange(GetListRecursive(client, path, fileNameFilter, directoryNameFilter));
break;
case FtpItemType.File:
if (fileNameFilter.IsMatch(name))
paths.Add(path);
break;
}
}
return paths;
}
private static string FtpPathCombine(string path1, string path2)
{
return string.Format("{0}/{1}", path1.TrimEnd('/'), path2.TrimStart('/'));
}
private static string GetRelativePath(string path, string basePath)
{
return path.Substring(basePath.Length).TrimStart('/', '\\');
}