2

I have a method that allows the user to specify a remote directory and a searchPattern with with to search files in the remote directory. Since I use third party libraries when retrieving the names of the files from the remote location, I am unable to take advantage of System.IO's Directory.GetFiles() routine which allows me specify the searchPattern when getting the files.

Basic String.Compare does not properly match the filename against the supplied pattern. Anyone know a more effective way of doing the matching please?

public IList<String> GetMatchingRemoteFiles(String SearchPattern, bool ignoreCase)
{
    IList<String> matchingFileNames = new List<String>();

   var RemoteFiles = thirdPartyTool.ftpClient.GetCurrentDirectoryContents();

    foreach( String filename in RemoteFiles)
     if( String.Compare(filename, SearchPattern, ignoreCase) == 0)
            matchingFileNames.Add(filename);

    return matchingFileNames;
}

Thanks in advance.

4
  • Please be specific with requirement. Like how do you call this method? Commented Apr 24, 2013 at 16:36
  • 1
    RegEx? There are thousands of search patterns online. Commented Apr 24, 2013 at 16:37
  • @NikhilAgrawal - I don't think it matters. The essence of the question seems to be "How do I glob-match strings" Commented Apr 24, 2013 at 16:38
  • what do you mean how do I call this method? How do I use Regex when the SearchPattern is an arbitrary string pattern provided by the user which I would like to match as closely as I can? Commented Apr 24, 2013 at 16:39

2 Answers 2

6

File matching with wildcards (*, ?) is called "glob" matching or "globbing". You can try converting the user-entered glob search to a regular expression, then using that. There's an example here:

Regex.Escape( wildcardExpression ).Replace( @"\*", ".*" ).Replace( @"\?", "." );

This would then get passed into a RegEx.Match() as the pattern, where you currently have String.Compare()

Sign up to request clarification or add additional context in comments.

2 Comments

so what happens in the case that the SearchPattern is something like 24April2013##_*.mp4 ?
I assume that # is supposed to represent a digit? You'd add another .Replace("#", @"\d") to the end to convert it to the regex version.
2

If you can specify what kinds of search strings this method will accept, you can use regular expressions. Here's an example which also uses Linq for brevity:

public IList<String> GetMatchingRemoteFiles(String SearchPattern, bool ignoreCase)
{
    var options = ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
    return thirdPartyTool.ftpClient.GetCurrentDirectoryContents()
                         .Where(fn => Regex.Matches(fn, SearchPattern, options))
                         .ToList();
}

Even if you cannot control what kinds of search strings this method accepts, it's still probably easier to convert the search string to a regular expression than write your own algorithm for matching the patterns. See Bobson's answer for details on how to do this.

1 Comment

This requires the end user who entered the search string to know regular expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.