0

I have a folder with three files and 2 sub folders. I am only trying to read the path including file names as string from this root folder, however only condition is that i want to exclude other directories located in same folder.

Folder structure:

Folder A
|
|
DirectoryA
fileA.xlsx, fileB.xlsx

What I Expect

All I want is to save path to fileA.xlsx and FileB.xlsx as string in list i.e.

[FolderA/fileA.xlsx, FolderA/fileB.xlsx]

My output:

my output also includes file contents from the sub directory as shown below which i dont want.

[FolderA/DirectoryA/somefile.txt, FolderA/fileA.xlsx, FolderA/fileB.xlsx]

What I tried

public List<String> getAllExcelFilesList() throws IOException {
    excelFilesList = new ArrayList<String>();
    excelFilesList = Files.walk(Paths.get(path_to_FolderA))
                     .filter(p -> p.getFileName().toString().startsWith("file"))
                     .filter(Files::isRegularFile)
                     .map(Path::toString)
                     .collect(Collectors.toList());
    LOG.info("all files saved to list for extraction");
    return excelFilesList;

I also tried following answer here Files.walk(), calculate total size but couldn't make my program to work. maybe i am missing something very insignificant.

EDIT:

from performance point of view it is better if program doesn't traverse through every item inside sub-folder because these sub-directories may include more than 1000 files in the future.

Radheya
  • 779
  • 1
  • 11
  • 41
  • @Derefacto no. i never heard about it. will take a look – Radheya Jun 17 '19 at 13:18
  • @Holger the result i get with my solution is that it includes path to items inside sub directories of my root folder, now it may happen that one of these directories might have 500 text files inside and my list will show me all the unwanted filepath names. – Radheya Jun 17 '19 at 13:27
  • Then, `startsWith("file")` is not sufficient as filter criteria? So you want `endsWith(".xlsx")` or `matches("file.*\\.xlsx")` instead? – Holger Jun 17 '19 at 13:30
  • `matches("file.*\\.xlsx")` this would be better because there will be other sub folders which will also have many .xlsx files and i would like to ignore these folders. only consider `matches("file.*\\.xlsx")` inside the root path folder – Radheya Jun 17 '19 at 13:33
  • 3
    Just to make it clear: if `path_to_FolderA` is what the name suggests and you want to iterate over the entries of that folder, without traversing any sub folder, just don’t use `Files.walk`, which is specifically made for traversing sub folders. Just use `Files.list(Paths.get(path_to_FolderA)) …` – Holger Jun 17 '19 at 13:36
  • @Holger oh okay. thanks for clarifying it. that is exactly what i want, definitely dont want to traverse through sub folders. thank you, i will see what i can do. – Radheya Jun 17 '19 at 13:40

1 Answers1

0

okay so i tried @holger's comment to get the solution and it works perfectly fine. Below is my solution:

public List<String> getAllExcelFilesList() throws IOException {
    excelFilesList = new ArrayList<String>();
    excelFilesList = Files.list(Paths.get(path_to_FolderA))
                     .filter(p -> p.getFileName().toString().startsWith("file"))
                     .filter(Files::isRegularFile)
                     .map(Path::toString)
                     .collect(Collectors.toList());
    LOG.info("all files saved to list for extraction");
    return excelFilesList;

I tried changing Files.walk with Files.list and now it works well :)

Radheya
  • 779
  • 1
  • 11
  • 41