File Sorting based on parameters

Many times our file directories are full of files that we may like to be sorted in some particular manner. Below is the code to sort the files as per the following ways:

  • Month Sorting
  • Alphabetic Sorting
  • Music Album Sorting
  • Extension Sorting
import java.io.File;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Scanner;
import java.util.TreeSet;

import org.blinkenlights.jid3.ID3Tag;
import org.blinkenlights.jid3.MP3File;
import org.blinkenlights.jid3.MediaFile;
import org.blinkenlights.jid3.v2.ID3V2_3_0Tag;

public class FileSorter {
    private Date getLastModified(File file) {
        return new Date(file.lastModified());
    }

    private String convert(Date file) throws ParseException {
        return new SimpleDateFormat("yyyy-MM").format(file.getTime());
    }

    public static void main(String[] args) {
        FileSorter p = new FileSorter();
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the folder path that you want sorted: ");
        String filePath = in.nextLine();
        System.out.println("Please enter what kind of sorting you want??");
        System.out.println("   1. Month Sorting");
        System.out.println("   2. Alphabetic Sorting");
        System.out.println("   3. Music Album Sorting");
        System.out.println("   5. Extension Sorting");
        try {
            int s = Integer.parseInt(in.nextLine());
            switch (s) {
            case 1:
                p.startSort(filePath);
                break;
            case 2:
                p.alphaSort(filePath);
                break;
            case 3:
                p.startMusicSort(filePath);
                break;
            case 5:
                p.extnSort(filePath);
                break;
            default:
                System.out.println("Invalid Option");
                break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getRange(String path) {
        String ret_String = "";
        for (int i = 97; i <= 122; i++) { if ((int) path.toLowerCase().charAt(0) >= i && (int) path.toLowerCase().charAt(0) <= (i + 3)) { ret_String = ((char) i + "-" + ((char) (i + 3))); break; } else if ((int) path.toLowerCase().charAt(0) >= 32 && (int) path.toLowerCase().charAt(0) <= 64) {
                ret_String = ("#0-9");
                break;
            } else {
                i += 3;
            }
        }
        return ret_String;
    }

    public void startSort(String filePath) throws Exception, ParseException {

        File dir = new File(filePath);
        File folder = null;
        String newFilePath = null;
        TreeSet ts = new TreeSet();

        if (dir.isDirectory()) {

            for (File file : dir.listFiles()) {
                newFilePath = convert(getLastModified(file));
                ts.add(newFilePath);
            }

            for (String o : ts) {
                folder = new File(filePath + "\" + o);
                folder.mkdirs();

                for (File file : dir.listFiles()) {
                    newFilePath = convert(getLastModified(file));
                    if (newFilePath.equals(o)) {
                        if (file.isFile()) {
                            file.renameTo(new File(filePath + "\" + o + "\" + file.getName()));
                        }
                    }
                }
            }
        }
    }

    public void alphaSort(String filePath) throws Exception, ParseException {

        File dir = new File(filePath);
        File folder = null;
        String newFilePath = null;
        TreeSet ts = new TreeSet();

        if (dir.isDirectory()) {

            for (File file : dir.listFiles()) {
                newFilePath = getRange(file.getName());
                ts.add(newFilePath);
            }

            for (String o : ts) {
                folder = new File(filePath + "\" + o);
                folder.mkdirs();

                for (File file : dir.listFiles()) {
                    newFilePath = getRange(file.getName());
                    if (newFilePath.equals(o)) {
                        if (file.isFile()) {
                            file.renameTo(new File(filePath + "\" + o + "\" + file.getName()));
                        }
                    }
                }
            }
        }
    }

    public void extnSort(String filePath) throws Exception, ParseException {

        File dir = new File(filePath);
        File folder = null;
        String newFilePath = null;
        TreeSet ts = new TreeSet();

        if (dir.isDirectory()) {

            for (File file : dir.listFiles()) {
                if (file.getName().lastIndexOf(".") > 0) {
                    newFilePath =
                        (file.getName().substring(file.getName().lastIndexOf(".") + 1,
                                                  file.getName().length())).toLowerCase();
                } else {
                    continue;
                }
                ts.add(newFilePath);
            }

            for (String o : ts) {
                folder = new File(filePath + "\" + o);
                folder.mkdirs();

                for (File file : dir.listFiles()) {
                    if (file.getName().lastIndexOf(".") > 0) {
                        newFilePath =
                            (file.getName().substring(file.getName().lastIndexOf(".") + 1,
                                                      file.getName().length())).toLowerCase();
                    } else {
                        continue;
                    }

                    if (newFilePath.equals(o)) {
                        if (file.isFile()) {
                            file.renameTo(new File(filePath + "\" + o + "\" + file.getName()));
                        }
                    }
                }
            }
        }
    }

    public void startMusicSort(String filePath) throws Exception, ParseException {

        File dir = new File(filePath);
        File folder = null;
        String newFilePath = null;
        TreeSet ts = new TreeSet();

        if (dir.isDirectory()) {

            for (File file : dir.listFiles()) {
                newFilePath = (getAlbumName(file));
                ts.add(newFilePath);
            }

            for (String o : ts) {
                folder = new File(filePath + "\" + o);
                folder.mkdirs();

                for (File file : dir.listFiles()) {
                    newFilePath = (getAlbumName(file));
                    if (newFilePath.equals(o)) {
                        if (file.isFile()) {
                            file.renameTo(new File(filePath + "\" + o + "\" + file.getName()));
                        }
                    }
                }
            }
        }
    }

    private String getAlbumName(File oSourceFile) {
        MediaFile oMediaFile = new MP3File(oSourceFile);
        ID3Tag[] aoID3Tag;
        try {
            aoID3Tag = oMediaFile.getTags();
            for (int i = 0; i < aoID3Tag.length; i++) {
                if (aoID3Tag[i] instanceof ID3V2_3_0Tag) {
                    ID3V2_3_0Tag oID3V2_3_0Tag = (ID3V2_3_0Tag) aoID3Tag[i];
                    try {
                        return ("".equals(oID3V2_3_0Tag.getAlbum().trim()) ? "Misc" :
                                oID3V2_3_0Tag.getAlbum().replaceAll(""", "")); // reads TYER frame
                    } catch (Exception e) {
                        e.getStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return "Misc";
    }

    public String toString() {
        return super.toString();
    }
}

Libraries can be found at:

Leave a Reply