Package com.atolsystems.atolutilities.tools

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

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;

/**
*
* @author seb
*/
public class FlattenFolder {
    static void showHelpAndExit(int exitStatus){
        System.out.print("FlattenFolder utility\n"+
                "Move all files from sub directories to the top directory\n"+
                "Usage: FlattenFolder <topFolderName>\n"
                );
      
        System.exit(exitStatus);
    }
    public static void main(String[] args) throws IOException {
        File topFolder;
        boolean guiMode=false;
        if(1!=args.length){
            guiMode=true;
            topFolder=AFileChooser.askForDirectory("Choose folder to flatten", false);
            if(null==topFolder)
                showHelpAndExit(-1);
            topFolder=topFolder.getCanonicalFile();
        }else{
            topFolder = (new File(args[0])).getCanonicalFile();
        }
        ATextScreenOutput logWin = null;
       
        if(guiMode){
            logWin= new ATextScreenOutput(null, false, true);
            logWin.setVisible(true);
            logWin.appendAndScroll("Processing "+topFolder+"\n");
        }
        flatten(topFolder,topFolder);
        if(guiMode){
            logWin.appendAndScroll("done");
        }else{
            System.exit(0);
        }
    }
    static int level=0;
    private static void flatten(File file, File topFolder) throws IOException {
        if(file.isFile()){
            if(0==file.getParentFile().compareTo(topFolder)) return;
            File dest=new File(topFolder, file.getName());
            while(dest.exists()){
                String ext=AFileUtilities.extractFileExtension(file.getName());
                String name = file.getName();
                if(!ext.isEmpty())
                    name=name.substring(0, name.length()-1-ext.length());
                name+=AStringUtilities.longToHex((new Random()).nextLong());
                if(!ext.isEmpty())
                    name=name+"."+ext;
                dest=new File(topFolder,name);
            }
            file.renameTo(dest);
        }else{
            for(int i=0;i<level;i++)
                System.out.print(" ");
            System.out.println("open  "+file.getCanonicalPath());
            level++;
            File[] list=file.listFiles();
            for(int i=0;i<list.length;i++)
                flatten(list[i],topFolder);
            level--;
            if(0!=file.compareTo(topFolder))
                file.delete();//if not top folder, then the folder is empty now, remove it
            for(int i=0;i<level;i++)
                System.out.print(" ");
            System.out.println("close "+file.getCanonicalPath())
        }
    }
}
TOP

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

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.