18 | ? | Matches any single character. Eg, "something?" collects any path that is named "something" plus any character. |
19 | * | Matches any characters up to the next slash. Eg, "*/*/something*" collects any path that has two directories, then a file or directory that starts with the name "something". |
20 | ** | Matches any characters. Eg, "**/something/**" collects any path that contains a directory named "something". |
21 | ! | A pattern starting with an exclamation point (!) causes paths matched by the pattern to be excluded, even if other patterns would select the paths. |
22 |
23 |
24 | When using `glob`, the search is done as efficiently as possible. Directories are not traversed if none of the search patterns can match them.
25 |
26 | Glob is also used when constructor parameters are specified:
27 |
28 | Paths paths = new Paths("/some/directory", "resources");
29 |
30 | ## Regex matching
31 |
32 | Regular expressions can be used to collect files and directories:
33 |
34 | Paths paths = new Paths();
35 | paths.regex("/some/directory", "images.*\\.jpg", "!.*/\\.svn/.*");
36 |
37 | Regex patterns that being with `!` caused matched paths to be excluded, even if other patterns would select the paths.
38 |
39 | Regular expressions have more expressive power, but it comes at a price. When using `regex`, the search is not done as efficiently as with `glob`. All directories and files under the root are traversed, even if none of the search patterns can match them.
40 |
41 | ## Pipe delimited patterns
42 |
43 | If `glob` or `regex` is passed only one parameter, it may be a root directory and then any number of search patterns, delimited by pipe (|) characters:
44 |
45 | Paths paths = new Paths();
46 | paths.glob("/some/directory|resources");
47 | paths.glob("/some/directory|images/**/*.jpg|!**/.svn/**");
48 |
49 | This is useful in cases where it is more convenient to use a single string to describe what files to collect.
50 |
51 | If `glob` is passed only one parameter that is not pipe delimited, or if only exclude patterns are specified (using the `!` character), then an additional search pattern of `**` is implied.
52 |
53 | ## Utility methods
54 |
55 | The `glob` and `regex` methods can be called repeatedly to collect paths from different root directories. Internally, a `Paths` instance holds all the paths matched and remembers each root directory where the search was performed. This greatly simplifies many tasks. The `Paths` class has utility methods for manipulating the paths, eg:
56 |
57 | Paths paths = new Paths();
58 | paths.glob("/some/directory", "**/*.jpg");
59 | paths.copyTo("/another/directory");
60 |
61 | This collects all JPG files in any directory under "/some/directory". It then copies those files to "/another/directory". Note that the directory structure under the root directory is preserved. Eg, if you had these files:
62 |
63 | /some/directory/stuff.jpg
64 | /some/directory/otherstuff.gif
65 | /some/directory/animals/cat.jpg
66 | /some/directory/animals/dog.jpg
67 | /some/directory/animals/giraffe.tga
68 |
69 | The result after the copy would be:
70 |
71 | /another/directory/stuff.jpg
72 | /another/directory/animals/cat.jpg
73 | /another/directory/animals/dog.jpg
74 |
75 | The `Paths` class has methods to copy, delete, and zip the paths. It also has methods to obtain the individual paths in various ways, so you can take whatever action you like:
76 |
77 | for (String fullPath : new Paths(".", "*.png")) { ... }
78 | for (String dirName : new Paths(".").dirsOnly().getNames()) { ... }
79 | for (File file : new Paths(".", "*.jpg").getFiles()) { ... )
80 |
--------------------------------------------------------------------------------
/project.yaml:
--------------------------------------------------------------------------------
1 | main: com.esotericsoftware.wildcard.Paths
2 | version: 1.04
3 |
--------------------------------------------------------------------------------
/src/com/esotericsoftware/wildcard/GlobScanner.java:
--------------------------------------------------------------------------------
1 |
2 | package com.esotericsoftware.wildcard;
3 |
4 | import java.io.File;
5 | import java.io.IOException;
6 | import java.util.ArrayList;
7 | import java.util.Iterator;
8 | import java.util.List;
9 |
10 | class GlobScanner {
11 | private final File rootDir;
12 | private final List