Path Matcher

Examples of using path matchers, such as IsFile, to select/match files and directories

File System Supported Matchers

Examples use the Filesystem's built-in matcher syntax to create a path matcher.

Some filesystems have support for native searching and matching of files. Normally using the PathSet API ({creating-pathsets} such as / and will use the native support, however in some cases there may be multiple supported syntax or the PathSet API does not support all the features of the native API. For example there could be an SQL like query language for a filesystem. To take advantage of these features paths and filesystems have a matcher method which takes a string as a query and another string as a syntax identifier.

There are two syntaxes that are supported by all filesystems and they are GLOB and REGEX which are typically used by the PathSets etc...

By default the GLOB syntax is used.


    import scalax.file.{Path, PathMatcher, FileSystem}

    // there are three factory methods that matchers
    // Path.matcher (instance method)
    // FileSystem.matcher

    // default type of matcher created is a glob matcher
    val InTmpDir: PathMatcher = Path("/tmp/file").matcher("/tmp/**")

    // If you can also create through the FileSystem
    val InBinDir: PathMatcher = FileSystem.default.matcher("/bin/*")

    // you can explicitly declare the GLOB matcher
    import PathMatcher.StandardSyntax.GLOB
    val StartsWithH: PathMatcher = FileSystem.default.matcher("**/H*", GLOB)

    // a Regex matcher is also available
    import PathMatcher.StandardSyntax.REGEX
    val ContainsVowel: PathMatcher = FileSystem.default.matcher(".*[aeiou].*", REGEX)

    // If a filesystem supports a filesystem specific sytax you can declare that
    val CustomSyntaxMatcher: PathMatcher = FileSystem.default.matcher("/tmp/@123", "customSyntax")

    // now demonstrate use
    // See FileSystem.matcher for details on creating a matcher
    Path("/tmp/file") match {
      case InTmpDir(path) => println("Path name is in tmp dir")
      case InBinDir(path) => println("File is in the bin dir")
      case StartsWithH(path) => println("Path name starts with an H")
      case ContainsVowel(path) => println("File contains a vowel")
      case CustomSyntaxMatcher(path) => println("CustomMatcher matched")
    }