Monday, May 12, 2008

Files sorter

/* if a file name is in the format -, this code can create respective directories and can move the files in the respective directories */

import java.io.File;

class SortingSongs {
public static void main(String[] args) {
// File dir = new File("D:\\Muvi");
// J:\Songs\B1- 1
// String dirToScan = "J:\\Songs\\B2 - 1";
String dirToScan = args[0];
File dir = new File(dirToScan);

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i = 0; i < children.length; i++) {
String[] splittedName = children[i].split("-");
// System.out.println(splittedName[0]);
File newDirForSong = new File(dirToScan + "\\"
+ splittedName[0].trim());
if (newDirForSong.isDirectory()) {
// System.out.println(splittedName[0] + "is a directory");
} else {
newDirForSong.mkdir();
}

// System.out.println(i + " --- " +children[i]);
File fileToBeMoved = new File(dirToScan + "\\" + children[i]);

fileToBeMoved.renameTo(new File(dirToScan + "\\"
+ splittedName[0].trim() + "\\" + children[i]));
}
}

//System.out.println(args[0]);
}
}

No comments: