'Java: Find .bat files in all folders of a drive (C:\ and others) [duplicate]

Is there a Java code that will look in every folder of a given drive and search for .bat files? And return them has a ArrayList of Files?

I'm trying to use Stream<Path> walk = Files.walk(this.rootPath) but it is only returning me the files on the root folder itself.



Solution 1:[1]

I made a simple script that do what you needed! :) It search in the given directory all the files, and if the given extension is equals to the file extension, it save the file name to a list that is returned later.

import java.io.*;
import java.util.*;

public class FileFinder{
    public static List<File> findFile(File file, String extension){
        File[] fileList = file.listFiles();

        List<File> returnList = new ArrayList<File>();

        if(fileList!=null){
            for(File fil : fileList){
                if(fil.isDirectory()){ findFile(fil, extension); }
                else if(fil.getName().endsWith(extension)) {
                    System.out.println(fil.getName());
                    returnList.add(fil);
                }
            }
        }

        return returnList;
    }

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the extension to be searched: ");
        String extension = scan.next();
        System.out.println("Enter the directory where to search: ");
        String directory = scan.next();
        
        List<File> fileList = findFile(new File(directory), extension);
        //System.out.println(fileList);
    }
}

P.S. this is an arrangment of this answer: Java - Search for files in a directory

Solution 2:[2]

Well I solved the problem. Just had to use the Files.walkFileTree.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 volpoh
Solution 2 gofuku