0

I had the following code:

Path dir = Paths.get("My root dir/Folder1");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {
        for (Path entry : stream) {
            // Do something
        }
} catch (IOException e) {
        //
}

Now the folder structure changed and I can have both:

My root dir
-- Folder1
---- MyFile.xml

or

My root dir
-- Folder1_MyFile.xml

So I would like to be able to use the following code:

Path dir = Paths.get("My root dir/Folder1");
String pattern = "Folder1/*.xml"; // or "Folder1_*.xml"
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, pattern)) {
        for (Path entry : stream) {
            // Do something
        }
}

In this case, I would only have to change the pattern variable, with a value of "Folder1/*.xml" or "Folder1_*.xml". The first one ("Folder1/*.xml") doesn't work because Files.newDirectoryStream is not recursive and only try to match the files/folders at the first level in the dir folder.

Is there any way to use the Files.newDirectoryStream(Path dir, String glob) method this way (filtering files recursively) or do I have to make myself a recursive method that retrieves ALL the files/folders and manually filter them ? (I would rather not use external libraries)

Thanks.

Skiing-Marmot
  • 118
  • 2
  • 8
  • And I have already seen the question [link](http://stackoverflow.com/questions/20987214/recursively-list-all-files-within-a-directory-using-nio-file-directorystream) but I would like not to list ALL the files but filter them using the already existing newDirectoryStream(Path dir, String glob) method. – Skiing-Marmot Jun 24 '14 at 12:13
  • What prevents you from using the method with the `glob` in the recursive approach in the linked question? – reto Jun 24 '14 at 12:23
  • First, my question was to know if it was possible to use the Files.newDirectoryStream(Path dir, String glob) method in a certain way to get what I wanted, without writing a recursive method (what I will do if the answer is that nothing else exists). – Skiing-Marmot Jun 24 '14 at 12:29
  • Second, I cannot directly use the solution in this link just adding the glob since the glob cannot be a path anyway. Yes I already knew I would have to write something like this if there is no way to use the Files.newDirectoryStream(Path dir, String glob) or similar with a path in the glob. – Skiing-Marmot Jun 24 '14 at 12:33
  • I suggest asking a new more carefully targeted question. – David Moles Jun 24 '14 at 18:54

0 Answers0