austinthemassive
austinthemassive

Reputation: 6625

Grabbing specific files in a folder

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!

Upvotes: 0

Views: 402

Answers (2)

sevensevens
sevensevens

Reputation: 1750

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);
        }
    }

Upvotes: 0

Related Questions