This question is related to a question I asked recently when I needed to do something very similar. I was trying to write a program that would get the file paths of all files within a certain number of levels in the directory and write them to a text file. Say, 5 levels below a specified starting directory. That program works exactly as I need. It gets the file paths of everything within 5 levels of subdirectory, including the starting directory, and writes them to a text file. Here is my code:
public class ScanFolder {
private static final int LEVELS = 5;
private static final String START_DIR = "C:/Users/Joe/Desktop/Test-Level1/";
private static final String REPORT_FILE = "C:/Users/Joe/Desktop/reports.txt";
public static void main(String[] args) throws IOException {
try (PrintWriter writer = new PrintWriter(REPORT_FILE, "UTF-8");
Stream<Path> pathStream = Files.walk(Paths.get(START_DIR), LEVELS)) {
pathStream.filter(Files::isRegularFile).forEach(writer::println);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
I realized today that for the idea I'm experimenting with, I also need a program that can get the file paths of all the folders in a specific level of subdirectory and write them to a text file. I don't want anything other than folder paths to be written to the text file(no .txts or anything of that nature). So instead of giving me the file paths of everything in the starting directory and everything down through the 5th level of subdirectory(starting directory being the 1st level, of course), it would give me the paths to all the folders that are five levels down from the first, and nothing in between.
Thanks in advance for all help. I'm really not very experienced in java and I'm fumbling around in the dark a lot of the time, so any help is appreciated. I don't want/mean to be or to seem overly dependent on help from this forum, but I often have no idea what I'm doing and need some guidance.