0

I am attempting to grab all of the mkv files in a folder and place their names in an array, yet my code only returns one value for some reason. I thought the FilenameFilter() would return all appropriate values, not just the first entry it encounters.

//Get a list of all mkv files in the extraction folder
File file = new File(extractedFolder);
File[] listoffiles = file.listFiles(new FilenameFilter() {
        public boolean accept(File file, String name) {
            return name.toLowerCase().endsWith(".mkv");
        }
});

Do I need to iterate through this code? Is my array only setup to handle one entry? Those are the only two potential problems I can see with this code, but both seem fine to my eye.

Java: Find .txt files in specified folder is a resource I was using as well.

EDIT: My filestructure has everything sitting in the same folder. That folder address is

C:\Users\user1\Documents\folder1\extraction files.  

Inside the "extraction files" folder, are 27 mkv files and 28 xml files (see image). The code I gave above only is pulling 12 files at a time. Do I need to specify a number when I initially create the array?

enter image description here

EDIT 2 Using this resource (Java process - unable to unzip zip file), I realized that a buffer I was using to unzip files (before grabbing them with the code above) was filling up and killing the process. Essentially, the issue was using a Runtime() instead of a ProcessBuilder() (which can prevent cmd from buffer overflows). Problem solved!

Community
  • 1
  • 1
austinthemassive
  • 5,907
  • 6
  • 21
  • 25
  • This should work. Could be problem with your files. Can you just loop through and check if result is different. – Lokesh Aug 04 '13 at 05:20
  • Agree that this seems correct. Perhaps the directory only contains one `.mkv` file. Seems like you really need to add a breakpoint and see what directory `extractedFolder` refers to. – Tim Bender Aug 04 '13 at 05:30
  • Just tested your code myself. Works correctly for me. – William Morrison Aug 04 '13 at 05:36
  • try to accept all files and check whether it actually list all files you expect – Rangi Lin Aug 04 '13 at 05:52
  • Are all your MKV files present within the top level **extracted** folder? Or is that the top level folder has a single file and then the remaining files may be within sub-folders of the extracted folder? In that case, you may want to recurse through the directory to get all the required mkv files – Prahalad Deshpande Aug 04 '13 at 06:16
  • can you show/print your directory structure? – Mehul Rathod Aug 04 '13 at 06:34
  • Have you tried changing the `FilenameFilter` to simply `return true;`? – oconnor0 Aug 05 '13 at 15:54
  • I have not attempted that. With xml files in the folder as well, wouldn't this cause it to pull in all the xml files as well? In any case, I did a clean and build and the code started working again. I'll test it over the next few days to see if it continues to work. – austinthemassive Aug 05 '13 at 16:53
  • The code above is only pulling 12 mkv files at a time. Is the initial size of an array defaulted to 12 or something? – austinthemassive Aug 07 '13 at 18:13
  • Make sure all files indeed have .mkv extension and non of them is hidden or write only. BTW, are these really files created in 31/12/1979? – Elist Aug 07 '13 at 18:38
  • This particular batch came from a camera that hasn't had the proper date set on it yet. None of the MKV's are hidden or read only. :/ – austinthemassive Aug 07 '13 at 20:04
  • Additionally, the file structure is C:\Users\User1\Documents\Folder1\mkv files. – austinthemassive Aug 08 '13 at 00:54

2 Answers2

1

Use can use FileUtils in commons-io to handle this for you.

Use the following method: http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#listFiles(java.io.File, java.lang.String[], boolean

cremersstijn
  • 2,375
  • 4
  • 28
  • 41
0

I've cut-n-pasted my file scanning code below. I don't see whats wrong with your version, but hopefully this will help. This way is a little different, and will hopefully work in your system.

    // FYI, m_dir is of type Path
    File dir = m_dir.toFile();

    if(dir.isDirectory())
    {
        for(File f : dir.listFiles())
        {
            // you probably want to replace the 2 lines below
            // with a check for .mkv files + processing
            logger.debug("File " + f.getName() + " found during inital search");
            m_listener.fileFoundInitialScan(f);
        }
    }
sevensevens
  • 1,703
  • 16
  • 27
  • Thanks for the idea. I did a clean and build and the code started working miraculously (which makes no sense, since I thought I did this several days ago). I'll definitely try this if problems crop up again. – austinthemassive Aug 05 '13 at 16:54