Tuesday, 25 February 2014

Java snippet - remove folders out, gen, .idea and files .iml from many Android projects in one click (intellij)

Here is simple java snippet to remove all not interesting files from Android projects.
This snippet removes, gen, out, .idea folders with files inside and also .iml file
 public class main {  
   private static final String[] FOLDERS = new String[]{"gen", "out", "bin", /*".idea"*/};  
   private static final String[] FILES_EXTENSION_TO_DEL = new String[]{/*".iml"*/};  
   private static final String PATH = "C:\\FolderX\\androidprojects";  
   private static final String[] IGNORE_EXTENSIONS = new String[]{".svn"};  
   private static ArrayList<File> dirToRemowe = new ArrayList<File>();  
   private static ArrayList<File> filesToRemowe = new ArrayList<File>();  
   public static void main(String[] args) {  
     new main().doStuff(PATH);  
   }  
   private void doStuff(String path) {  
       String currentPath = PATH;  
       System.out.println("[PATH] " + currentPath);  
       File file = new File(currentPath);  
       iterateFolders(file, false);  
       deleteElements(filesToRemowe);  
       deleteElements(dirToRemowe);  
   }  
   private void deleteElements(ArrayList<File> filesToRemowe) {  
     Collections.reverse(filesToRemowe);  
     for (File f : filesToRemowe) {  
       System.out.println("f[] To remove: " + f.getAbsolutePath());  
       try {  
         FileInputStream in = new FileInputStream(f);  
         in.close();  
       } catch (FileNotFoundException e) {  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
       System.out.println(f.delete() + "");  
       try {  
         Thread.sleep(5);  
       } catch (InterruptedException e) {  
         e.printStackTrace();  
       }  
     }  
   }  
   public static void iterateFolders(File file, boolean interesting) {  
     File[] files = file.listFiles();  
     for (File f : files) {  
       String absolutePath = f.getAbsolutePath();  
       //if interesting folder true so add all to remove list  
       if (interesting) {  
         if (f.isFile()) {  
           if (!filesToRemowe.contains(f))  
             filesToRemowe.add(f);  
         } else if (f.isDirectory()) {  
           if (!dirToRemowe.contains(f))  
             dirToRemowe.add(f);  
           iterateFolders(f, true);  
         }  
       } else {  
         //default searching folders  
         //add file only with specific name, adding any file will be called when interesting == true  
         if (f.isFile()) {  
           for (String file_extension : FILES_EXTENSION_TO_DEL) {  
             if (f.getName().endsWith(file_extension)) {  
               if (!filesToRemowe.contains(f))  
                 filesToRemowe.add(f);  
             }  
           }  
         } else if (f.isDirectory()) {  
           interesting = false;  
           for (String s : FOLDERS) {  
             if (f.getName().equals(s))  
               if (!dirToRemowe.contains(f)) {  
                 dirToRemowe.add(f);  
                 interesting = true;  
               }  
           }  
           for (String s : IGNORE_EXTENSIONS)  
             if (f.getName().equals(".svn"))  
               continue;//and dont go inside svn, iterate again next  
           iterateFolders(f, interesting);  
           //resert interesting flag on exit from this folder  
           interesting = false;  
         }  
       }  
     }  
   }  
 }