0

At the moment I am using this following code to check my test data folder for m3u (playlist) file:

String filename = "test-data\\non-existent-playlist.m3u";
        boolean isHeaderValid = M3UReader.isValidHeader(filename);
        System.out.println(filename + "header tested as "+ isHeaderValid);

I would like to be able to make the program scan the whole of the c drive or removable storage for playlists files but I cannot find any similar programs that do this.

tshepang
  • 12,111
  • 21
  • 91
  • 136
JWayne93
  • 89
  • 1
  • 1
  • 10
  • duplicate question http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder -- google: java read folder contents – fonZ Nov 19 '13 at 16:54
  • @fonZ, If folder name is unknown than your link question will not be applicable. – Masudul Nov 19 '13 at 16:59
  • @Masud there is no difference in searching a folder or the root of a hard drive, those are two exact the same operations, the difference is the path. – fonZ Nov 19 '13 at 21:45

1 Answers1

1

Use Files.walkFileTree. This will scan whole drive and search your desired file name.

public static void main(String[] args) throws IOException {
    Path startingDir = Paths
        .get("C:\\");
    Files.walkFileTree(startingDir, new FindJavaVisitor());
  }

  private static class FindJavaVisitor extends SimpleFileVisitor<Path> {

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

      if (file.toString().contains("non-existent-playlist.m3u")) {
        System.out.println(file.getFileName());
      }
      return FileVisitResult.CONTINUE;
    }
  }
Masudul
  • 21,823
  • 5
  • 43
  • 58