如何判断远程文件路径是否对应于一个过滤器
How to tell if a remote file path corresponds to one filter
目前,我已经在远程文件夹中获取了这些路径。
/folder 1/subfolder 2/filea.jpg
/folder 1/subfolder 2/fileb.pdf
/folder 1/subfolder 3/filea.jpg
我已经设置了过滤器来决定我是否应该下载路径列表中的文件,基于 windows 文件资源管理器,例如
/folder ?/subfolder ?/*.jpg
/*/*/abc.*
/*/*/*.*
决定是否下载的最佳方法是什么?
谢谢
一种方法是结合使用 Directory.EnumerateDirectories()
and Directory.GetFiles()
方法,这两种方法都接受一个 string searchPattern
参数。
注意:要使用 Directory
class,您需要使用文件顶部的 System.IO
命名空间:
using System.IO;
我相信您必须分别枚举每个目录,因为反斜杠字符在文件名或目录名中是非法的。
例如,您可以使用一种方法,该方法采用 rootPath
(您正在搜索的最顶层目录)和 returns 驻留在名为的目录中的所有“*.jpg”文件"subfolder ?"(其中 ?
是单个字符的占位符)仅当子文件夹直接位于名为 "folder ?":
的目录下时
/// <summary>
/// Searches for files using the pattern "/folder ?/subfolder ?/*.jpg"
/// </summary>
/// <param name="rootPath">The directory in which to begin the search</param>
/// <returns>A list of file paths that meet the criteria</returns>
public static List<string> GetDownloadableFiles(string rootPath)
{
var files = new List<string>();
// First find all the directories that match 'folder ?', anywhere under 'rootPath'
foreach (var directory in Directory.EnumerateDirectories(rootPath, "folder ?",
SearchOption.AllDirectories))
{
// Now find all directories directly under 'folder ?' named 'subfolder ?'
foreach (var subDir in Directory.EnumerateDirectories(directory, "subfolder ?"))
{
// And add the file path for all '*.jpg' files to our list
files.AddRange(Directory.GetFiles(subDir, "*.jpg"));
}
}
return files;
}
在使用中,你会做这样的事情:
List<string> downloadableFiles = GetDownloadableFiles(@"\server\share");
解决您的特定示例的方法完成后,我们现在可以看到如何使它更通用,以便客户端可以传入任何类型的搜索字符串。
如果我们说搜索字符串中的定界符是正斜杠(/
),那么我们可以让用户传入字符串,我们可以在该字符上拆分它。这将为我们提供一组目录搜索模式和一个文件搜索模式(最后一项)。
一旦我们这样做了,我们就可以遍历搜索模式,获取与当前模式匹配的目录,并在每次迭代时将它们存储在一个临时列表中,直到我们到达最后一部分(即文件搜索模式) ),我们从中获取与该模式匹配的文件。
例如:
/// <summary>
/// Searches for files using the pattern defined in 'searchPattern'
/// Example search patterns: "/project ?/photoshoot ?/flowers ?/*.jpg"
/// "/project ?/plans ?/*.pdf"
/// "/project ?/*.txt"
/// </summary>
/// <param name="rootPath">The directory in which to begin the search</param>
/// <param name="searchPattern">The directory and file search pattern</param>
/// <returns>A list of file paths that meet the criteria</returns>
public static List<string> GetDownloadableFiles(string rootPath, string searchPattern)
{
if (!Directory.Exists(rootPath))
throw new DirectoryNotFoundException(nameof(rootPath));
if (searchPattern == null)
throw new ArgumentNullException(nameof(searchPattern));
var files = new List<string>();
var searchParts = searchPattern.Split(new[] {'/'},
StringSplitOptions.RemoveEmptyEntries);
// This will hold the list of directories to search, and
// will be updated on each iteration of our loop below.
// We start with just one item: the 'rootPath'
var searchFolders = new List<string> {rootPath};
for (int i = 0; i < searchParts.Length; i++)
{
var subFolders = new List<string>();
foreach (var searchFolder in searchFolders)
{
// If we're at the last item, it's the file pattern, so add files
if (i == searchParts.Length - 1)
{
files.AddRange(Directory.GetFiles(searchFolder, searchParts[i]));
}
// Otherwise, add the sub directories for this pattern
else
{
subFolders.AddRange(Directory.GetDirectories(searchFolder,
searchParts[i]));
}
}
// Reset our search folders to use the list from the latest pattern
searchFolders = subFolders;
}
return files;
}
用法示例:
var files = GetDownloadableFiles(@"\server\share", "/folder ?/subfolder ?/*.jpg");
目前,我已经在远程文件夹中获取了这些路径。
/folder 1/subfolder 2/filea.jpg
/folder 1/subfolder 2/fileb.pdf
/folder 1/subfolder 3/filea.jpg
我已经设置了过滤器来决定我是否应该下载路径列表中的文件,基于 windows 文件资源管理器,例如
/folder ?/subfolder ?/*.jpg
/*/*/abc.*
/*/*/*.*
决定是否下载的最佳方法是什么?
谢谢
一种方法是结合使用 Directory.EnumerateDirectories()
and Directory.GetFiles()
方法,这两种方法都接受一个 string searchPattern
参数。
注意:要使用 Directory
class,您需要使用文件顶部的 System.IO
命名空间:
using System.IO;
我相信您必须分别枚举每个目录,因为反斜杠字符在文件名或目录名中是非法的。
例如,您可以使用一种方法,该方法采用 rootPath
(您正在搜索的最顶层目录)和 returns 驻留在名为的目录中的所有“*.jpg”文件"subfolder ?"(其中 ?
是单个字符的占位符)仅当子文件夹直接位于名为 "folder ?":
/// <summary>
/// Searches for files using the pattern "/folder ?/subfolder ?/*.jpg"
/// </summary>
/// <param name="rootPath">The directory in which to begin the search</param>
/// <returns>A list of file paths that meet the criteria</returns>
public static List<string> GetDownloadableFiles(string rootPath)
{
var files = new List<string>();
// First find all the directories that match 'folder ?', anywhere under 'rootPath'
foreach (var directory in Directory.EnumerateDirectories(rootPath, "folder ?",
SearchOption.AllDirectories))
{
// Now find all directories directly under 'folder ?' named 'subfolder ?'
foreach (var subDir in Directory.EnumerateDirectories(directory, "subfolder ?"))
{
// And add the file path for all '*.jpg' files to our list
files.AddRange(Directory.GetFiles(subDir, "*.jpg"));
}
}
return files;
}
在使用中,你会做这样的事情:
List<string> downloadableFiles = GetDownloadableFiles(@"\server\share");
解决您的特定示例的方法完成后,我们现在可以看到如何使它更通用,以便客户端可以传入任何类型的搜索字符串。
如果我们说搜索字符串中的定界符是正斜杠(/
),那么我们可以让用户传入字符串,我们可以在该字符上拆分它。这将为我们提供一组目录搜索模式和一个文件搜索模式(最后一项)。
一旦我们这样做了,我们就可以遍历搜索模式,获取与当前模式匹配的目录,并在每次迭代时将它们存储在一个临时列表中,直到我们到达最后一部分(即文件搜索模式) ),我们从中获取与该模式匹配的文件。
例如:
/// <summary>
/// Searches for files using the pattern defined in 'searchPattern'
/// Example search patterns: "/project ?/photoshoot ?/flowers ?/*.jpg"
/// "/project ?/plans ?/*.pdf"
/// "/project ?/*.txt"
/// </summary>
/// <param name="rootPath">The directory in which to begin the search</param>
/// <param name="searchPattern">The directory and file search pattern</param>
/// <returns>A list of file paths that meet the criteria</returns>
public static List<string> GetDownloadableFiles(string rootPath, string searchPattern)
{
if (!Directory.Exists(rootPath))
throw new DirectoryNotFoundException(nameof(rootPath));
if (searchPattern == null)
throw new ArgumentNullException(nameof(searchPattern));
var files = new List<string>();
var searchParts = searchPattern.Split(new[] {'/'},
StringSplitOptions.RemoveEmptyEntries);
// This will hold the list of directories to search, and
// will be updated on each iteration of our loop below.
// We start with just one item: the 'rootPath'
var searchFolders = new List<string> {rootPath};
for (int i = 0; i < searchParts.Length; i++)
{
var subFolders = new List<string>();
foreach (var searchFolder in searchFolders)
{
// If we're at the last item, it's the file pattern, so add files
if (i == searchParts.Length - 1)
{
files.AddRange(Directory.GetFiles(searchFolder, searchParts[i]));
}
// Otherwise, add the sub directories for this pattern
else
{
subFolders.AddRange(Directory.GetDirectories(searchFolder,
searchParts[i]));
}
}
// Reset our search folders to use the list from the latest pattern
searchFolders = subFolders;
}
return files;
}
用法示例:
var files = GetDownloadableFiles(@"\server\share", "/folder ?/subfolder ?/*.jpg");