Created
September 26, 2012 12:29
-
-
Save timpulver/3787737 to your computer and use it in GitHub Desktop.
[Processing] Delete all folders with only one file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String parentFolder = "M:\\INSERT_PARENT_DIR_HERE"; | |
| void setup(){ | |
| File tmpDir; | |
| File fParentFolder = new File(parentFolder); | |
| println("Scanning " + fParentFolder.getAbsolutePath()); | |
| println("Folder exists: " + fParentFolder.exists()); | |
| File[] folderList = fParentFolder.listFiles(); | |
| println("Number of files/folders: " + folderList.length); | |
| println("-----------------------------------"); | |
| for(int i=0; i<folderList.length; i++){ | |
| /* | |
| tmpDir = new File(folderList[i]); | |
| if(tmpDir.isDirectory() && tmpDir.list().length < 2){ | |
| println("Deleting directory: " + deleteDir(tmpDir)); | |
| } | |
| */ | |
| if(folderList[i].isDirectory() && folderList[i].list().length < 2){ | |
| println("Deleting directory: " + folderList[i].getAbsolutePath() + "\t\t" + deleteDir(folderList[i])); | |
| } | |
| } | |
| } | |
| // Deletes all files and subdirectories under dir. | |
| // Returns true if all deletions were successful. | |
| // If a deletion fails, the method stops attempting to delete and returns false. | |
| public static boolean deleteDir(File dir) { | |
| if (dir.isDirectory()) { | |
| String[] children = dir.list(); | |
| for (int i=0; i<children.length; i++) { | |
| boolean success = deleteDir(new File(dir, children[i])); | |
| if (!success) { | |
| return false; | |
| } | |
| } | |
| } | |
| // The directory is now empty so delete it | |
| return dir.delete(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment