Package com.atolsystems.atolutilities.tools

Source Code of com.atolsystems.atolutilities.tools.DelEmpty

package com.atolsystems.atolutilities.tools;

import com.atolsystems.atolutilities.AFileChooser;
import com.atolsystems.atolutilities.AFileUtilities;
import com.atolsystems.atolutilities.AStringUtilities;
import com.atolsystems.atolutilities.ATextScreenOutput;
import com.atolsystems.atolutilities.LoggingWindow;
import java.io.File;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author seb
*/
public class DelEmpty {
    static void showHelpAndExit(int exitStatus){
        println("DelEmpty utility\n"+
                "Delete empty folders recursively\n"+
                "Usages: - Console mode: DelEmpty <topFolderName>\n"+
                "        - GUI mode: DelEmpty"
                );
        if(guiMode){
            while(true){
                try {
                    Thread.sleep(Integer.MAX_VALUE);
                } catch (InterruptedException ex) {
                    Logger.getLogger(DelEmpty.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        System.exit(exitStatus);
    }
    static boolean guiMode=false;
    static ATextScreenOutput logWin = null;
    public static void main(String[] args) throws IOException {
        File topFolder;
        if(0==args.length){
            logWin= new ATextScreenOutput(null, false, true);
            logWin.setVisible(true);
            guiMode=true;
            topFolder=AFileChooser.askForDirectory("Choose top folder", false);
            if(null==topFolder)
                showHelpAndExit(-1);
            topFolder=topFolder.getCanonicalFile();
        }else if(1==args.length){
            topFolder = (new File(args[0])).getCanonicalFile();
        }else{
            showHelpAndExit(1);
            return;
        }
        println("Processing "+topFolder);
        delEmptyFolders(topFolder);
        println("done");
    }
    private static void print(String msg){
        if(guiMode){
            logWin.appendAndScroll(msg);
        }else{
            System.out.print(msg);
        }
    }
    private static void println(String msg){
        if(guiMode){
            logWin.appendAndScroll(msg+"\n");
        }else{
            System.out.println(msg);
        }
    }
    static int level=0;
    private static void delEmptyFolders(File topFolder) throws IOException {
        for(int i=0;i<level;i++)
            print(" ");
        println("open  "+topFolder.getCanonicalPath());
        level++;
        File[] list=topFolder.listFiles();
        for(File inTop:list){
            if(inTop.isDirectory()){
                if(inTop.listFiles().length!=0){
                    delEmptyFolders(inTop);
                }
                if(inTop.listFiles().length==0){//re-test file count in case sub folders have been deleted
                    println("delete "+inTop);
                    AFileUtilities.removeDirectory(inTop);
                }
            }
        }
           
        level--;
        for(int i=0;i<level;i++)
            print(" ");
        println("close "+topFolder.getCanonicalPath())
    }
}
TOP

Related Classes of com.atolsystems.atolutilities.tools.DelEmpty

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.