0

I try to remove file in directory without file which path is in my array

    public static boolean deleteDir(File dir, List<String> exclusionList) throws IOException {
    if (exclusionList != null && exclusionList.contains(dir.getCanonicalPath())) { // skip file

        System.out.println("Skipped: " + dir.getCanonicalPath());
        return true;
    }

    System.out.println("Deleting: " + dir.getCanonicalPath());
    if (dir.isDirectory()) {
        File[] children = dir.listFiles();
        boolean success = true;
        for (File element : children) {
            if (!deleteDir(element, exclusionList)) {
                success = false;
            }
        }
        return success;
    }

    return dir.delete();
}

this is my function to delete and is work fine to delete file like .txt .yml etc but when it must delete folder(folder content remove perflecty) but folder is exists, and i have a lot of empty folders ;/

Cœur
  • 37,241
  • 25
  • 195
  • 267
lenyAxe
  • 55
  • 1
  • 7

2 Answers2

1

The issue is caused by how directories will get handled:

System.out.println("Deleting: " + dir.getCanonicalPath());
if (dir.isDirectory()) {
    File[] children = dir.listFiles();
    boolean success = true;
    for (File element : children) {
        if (!deleteDir(element, exclusionList)) {
            success = false;
        }
    }
    return success;   // <= for directories the method returns here
}

return dir.delete();

In directories the method will recursively call deleteDir for all child-elements and check whether all children were deleted successfully. Afterwards the method simply returns, without deleting the directory itself. A simple workaround would be to terminate only if the deletion of failed for a certain child-element:

System.out.println("Deleting: " + dir.getCanonicalPath());
if (dir.isDirectory()) {
    File[] children = dir.listFiles();
    boolean success = true;
    for (File element : children) {
        if (!deleteDir(element, exclusionList)) {
            success = false;
        }
    }

    // return only if some child couldn't be deleted.
    if(!success)
        return false;
}

// delete the directory itself (or the file, if a file is passed as parameter)
return dir.delete();
  • i change this but still i have empty folder, all .txt files remove correctly but folder still not delete ;/ – lenyAxe May 17 '17 at 23:33
  • @lenyAxe Thats strange. For me this works just fine. All directories except the excluded ones get deleted. Did you get any IOException or other hint what went wrong? Probably an issue with file-permissions? –  May 17 '17 at 23:43
  • permissions iss fine because when i use those credentialns and connect with filezilla i can remove all files, any exception ;/ i run this app on linux – lenyAxe May 17 '17 at 23:51
  • Edit. Your code works fine, i foget to change name in method XD thanks ;) – lenyAxe May 18 '17 at 11:34
  • @lenyAxe glad I could help :) –  May 18 '17 at 12:06
0

If I understand your question correctly you have many empty folders after deleting files, such as .txt, and you want them gone?

Here's an idea:

After returning from deleting a child (element) check if its a directory and the number of files is zero in that directory, then apply:

FileUtils.deleteDirectory(directory)

if (element is a directory and is empty) {
    FileUtils.deleteDirectory(element);
}

That should be approximately what you want. Go ahead and work out the logic in the if statement and it should delete each empty directory in your for loop.

Note FileUtils is part of http://commons.apache.org/proper/commons-io/

Tr1gZer0
  • 1,482
  • 11
  • 18