Delete all empty folders in Java

I would like to write a function that removes all empty folders, with the ability to ignore certain file types (valid file types are stored in a hashmap) and tell them if they should search inside directories.

Call:

HashMap<String, Boolean> allowedFileTypes = new HashMap<String, Boolean>();
allowedFileTypes.put("pdf", true);
deleteEmptyFolders("ABSOLUTE PATH", allowedFileTypes, true);

      

Function:

public static void deleteEmptyFolders(String folderPath, HashMap<String, Boolean> allowedFileTypes, boolean followDirectory) {

    File targetFolder = new File(folderPath);
    File[] allFiles = targetFolder.listFiles();


    if (allFiles.length == 0)
        targetFolder.delete();

    else {
        boolean importantFiles = false;

        for (File file : allFiles) {

            String fileType = "folder";
            if (!file.isDirectory())
                fileType = file.getName().substring(file.getName().lastIndexOf('.') + 1);

            if (!importantFiles)
                importantFiles = (allowedFileTypes.get(fileType) != null);

            if (file.isDirectory() && followDirectory)
                deleteEmptyFolders(file.getAbsolutePath(), allowedFileTypes, followDirectory);
        }

        // if there are no important files in the target folder
        if (!importantFiles)
            targetFolder.delete();
    }
}

      

The problem is that nothing happens even if it goes through all the folders to the end. Is this a good approach or am I missing something?

+3


source to share


2 answers


This code snippet recursively removes all empty folders or directory:



public class DeleteEmptyDir {
private static final String FOLDER_LOCATION = "E:\\TEST";
private static boolean isFinished = false;

public static void main(String[] args) {

    do {
        isFinished = true;
        replaceText(FOLDER_LOCATION);
    } while (!isFinished);
}

private static void replaceText(String fileLocation) {
    File folder = new File(fileLocation);
    File[] listofFiles = folder.listFiles();
    if (listofFiles.length == 0) {
        System.out.println("Folder Name :: " + folder.getAbsolutePath() + " is deleted.");
        folder.delete();
        isFinished = false;
    } else {
        for (int j = 0; j < listofFiles.length; j++) {
            File file = listofFiles[j];
            if (file.isDirectory()) {
                replaceText(file.getAbsolutePath());
            }
        }
    }
}
}

      

+1


source


You can use code to delete empty folders using Java.



public static long deleteFolder(String dir) {

        File f = new File(dir);
        String listFiles[] = f.list();
        long totalSize = 0;
        for (String file : listFiles) {

            File folder = new File(dir + "/" + file);
            if (folder.isDirectory()) {
                totalSize += deleteFolder(folder.getAbsolutePath());
            } else {
                totalSize += folder.length();
            }
        }

        if (totalSize ==0) {
            f.delete();
        }

        return totalSize;
    }

      

0


source







All Articles