Output of folders inside which there is a desired file

Viewed 83

In my program, the following are implemented there is a listView in it, I bring out the folders that have certain data in the inside. But some of them are empty. I'm trying to make a check that it does not display empty folders for me in the listView

public static File dir= new File(Environment.getExternalStorageDirectory().getPath()+"/Sales/Archive");

ArrayList<String> theNamesOfFiles = new ArrayList<>();
        File[] filelist = dir.listFiles();
        for (int i = 0; i < filelist.length; i++) {
            long diff = (new Date().getTime() - filelist[i].lastModified())  / 144 / 144 / 144;
            Arrays.sort(filelist, Comparator.comparingLong(File::lastModified).reversed());
            if (diff < 72)
                theNamesOfFiles.add(filelist[i].getName());
        }

After some attempts, it turned out that he goes into every file there looking for what I need. But he displays everything the same without them

File Path = new File((new File(ArchiveDir)).getParent());
        File[] listFile = Path.listFiles();
        for (File file : listFile) {
            File[] listFile2 = file.listFiles();
            for (File file2 : listFile2){
                if (file.isDirectory() && file2.getName().toUpperCase().contains("S")) {
                    File[] filelist = Path.listFiles();
                    for (int i = 0; i < filelist.length; i++) {
                        long diff = (new Date().getTime() - filelist[i].lastModified()) / 144 / 144 / 144;
                        Arrays.sort(filelist, Comparator.comparingLong(File::lastModified).reversed());
                        if (diff < 72)
                            theNamesOfFiles.add(filelist[i].getName());
                    }
                }
            }
        }
2 Answers

Here is the solution to my problem. But there is a tiny nuance in it. My program creates a file every time it starts (data backup) and for some reason this code only skips the created file.

int i = 0;

File Path = new File((new File(ArchiveDir)).getParent());
        File[] listFile = Path.listFiles();
        for (File file : listFile) {
            i ++;
            File[] listFile2 = file.listFiles();
            for (File file2 : listFile2){
                if (file2.getName().toUpperCase().contains("S")) {
                    long diff = (new Date().getTime() - listFile[i].lastModified()) / 144 / 144 / 144;
                    Arrays.sort(listFile, Comparator.comparingLong(File::lastModified).reversed());
                    if (diff < 72) {
                        theNamesOfFiles.add(listFile[i].getName());
                    }
                }
            }
        }

As I understand,theNamesOfFiles is a list of folders. And it contains individual folder's names. So, if you want to check if that folder is empty or not it can be done by file.length() function. Which returns 0 if the file is empty.

Here's the solution that I think will work best for you.

 if (file.isDirectory() && file2.getName().toUpperCase().contains("S")) {
                    File[] filelist = file2.listFiles(); //update
                    for (int i = 0; i < filelist.length; i++) {
                        long diff = (new Date().getTime() - filelist[i].lastModified()) / 144 / 144 / 144;
                        Arrays.sort(filelist, Comparator.comparingLong(File::lastModified).reversed());
                        if (diff < 72)
                          if(filelist[i].length() !=0) //update
                            theNamesOfFiles.add(filelist[i].getName());//update
                    }
                }

This will ensure that the element present at the index i is not empty. And if it is empty it will be skipped. I won't be added to your list.

Related