What I need to do if Search a folder say C:\\example
I then need to go through each file and check to see if it matches a few start characters so if fil
Usage:
//Searches file names (start with "temp" and extension ".txt")
//in the current directory and subdirectories recursively
Path initialPath = Paths.get(".");
PathUtils.searchRegularFilesStartsWith(initialPath, "temp", ".txt").
stream().forEach(System.out::println);
Source:
public final class PathUtils {
private static final String startsWithRegex = "(? searchRegularFilesStartsWith(final Path initialPath,
final String fileName, final String fileExt) throws IOException {
return searchRegularFiles(initialPath, startsWithRegex + fileName, fileExt);
}
public static List searchRegularFilesEndsWith(final Path initialPath,
final String fileName, final String fileExt) throws IOException {
return searchRegularFiles(initialPath, fileName + endsWithRegex, fileExt);
}
public static List searchRegularFilesAll(final Path initialPath) throws IOException {
return searchRegularFiles(initialPath, "", "");
}
public static List searchRegularFiles(final Path initialPath,
final String fileName, final String fileExt)
throws IOException {
final String regex = String.format(containsRegex, fileName, fileExt);
final Pattern pattern = Pattern.compile(regex);
try (Stream walk = Files.walk(initialPath.toRealPath())) {
return walk.filter(path -> Files.isRegularFile(path) &&
pattern.matcher(path.toString()).find())
.collect(Collectors.toList());
}
}
private PathUtils() {
}
}
Try startsWith regex for \txt\temp\tempZERO0.txt:
(?
Try endsWith regex for \txt\temp\ZERO0temp.txt:
temp(?=[\\.\\n])(?:[^\/\\]*(?=((?i)\.txt(?!.))))
Try contains regex for \txt\temp\tempZERO0tempZERO0temp.txt:
temp(?:[^\/\\]*(?=((?i)\.txt(?!.))))