If you have your directory depth more than 255 characters as created in my previous article, file explorer e.g windows explorer will not be able to delete it showing error it has more length then expected or so. In order to delete those folders which has folder depth more than 255 character, copy paste the code below to your favorite editor, save it with the classname you wish, and change the directory mentioned in the code to the target root directory where you want to apply this repetitive deletion.
import java.io.File;
public class ThousandDirectory {
public static void main(String[] args) {
String abc = "D:\\ThousandDirectory";
String onlyDirectory = "ThousandDirectory";
File baseDirectory = new File(abc);
try {
if (baseDirectory.isDirectory()) {
for (int i = 0; i < 1000; i++) {
if (i != 0) {
abc = baseDirectory.getParentFile().getAbsolutePath();
}
File file = new File(abc);
if (file.list().length == 0) {
baseDirectory = new File(abc);
if (file.delete()) {
System.out.println(i);
}
i--;
} else if (file.isDirectory()) {
abc = baseDirectory.getAbsolutePath() + "\\" + onlyDirectory;
baseDirectory = new File(abc);
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Voila You are done. The directories your file explorer(e.g. windows explorer) could not delete are deleted.
Enjoy your deletion.