MatchFinder
[code="java"]public class MatchFinder { //static List matchNames = new ArrayList(); static List nonMatchNames = new ArrayList(); public static void main(String[] args) throws Exception { List matchNames = new ArrayList(); //enumerate(new File("I:/_backup/_bad")); //enumerate(new File("D:/_bad")); //enumerate(new File("E:/badmintons"), matchNames); //enumerate(new File("F:/badmintons"), matchNames); /**/ readIndexFromFile("I:/_backup/_bad/bad.index.txt", matchNames); /* readIndexFromFile("F:/louis/badminton-index/Home.BY.E.txt", matchNames); readIndexFromFile("F:/louis/badminton-index/Home.Louis.E.txt", matchNames); readIndexFromFile("F:/louis/badminton-index/Home.Louis.F.txt", matchNames);*/ //enumerate(new File("F:/_bad/badmintons"), matchNames); Collections.sort(matchNames); //, new MatchInfoCompator() Log.prt(matchNames); //writeToExcel(matchNames, "c:/tmp/matches.xls"); } static void writeToExcel(List matchNames, String fileName) { FileOutputStream fos = null; try { MWorkbook workbook = new MWorkbook(); //write data MSheet sheet = workbook.createSheet("Match"); MRow row = sheet.createRow(0); //Create Header String[] headers = {"Year", "Match", "Item", "Player1", "Player2", "File Name", "Absolute Path"}; for (int i = 0; i < headers.length; i++) { MCell cell = row.createCell((short) i); cell.setMCellValue(headers[i], true); } for (int i = 0; i < matchNames.size(); i ++ ) { MatchInfo info = matchNames.get(i); row = sheet.createRow(1 + i); MCell cell = row.createCell((short) 0); cell.setMCellValue(info.year); cell = row.createCell((short) 1); cell.setMCellValue(info.match); cell = row.createCell((short) 2); cell.setMCellValue(info.item); cell = row.createCell((short) 3); cell.setMCellValue(info.player1); cell = row.createCell((short) 4); cell.setMCellValue(info.player2); cell = row.createCell((short) 5); cell.setMCellValue(info.fileName); cell = row.createCell((short) 6); cell.setMCellValue(info.absolutePath); } //Set column to proper size for (int i = 0; i < headers.length; i++) { sheet.autoSizeColumn((short) i); } //Save to file fos = new FileOutputStream(fileName); workbook.write(fos); } catch (FileNotFoundException e1) { Log.error(e1); } catch (IOException e2) { Log.error(e2); } finally { if (fos != null) try { fos.close(); } catch (IOException e) { Log.error(e); } } } public static void enumerate(File directory, List matchNames) throws Exception { File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) enumerate(file, matchNames); else search(file, matchNames); } } static void search(File file, List matchNames) throws IOException { String fileName = file.getName(); if (!isValidMatchFile(fileName)) { //Log.prt("*** " + fileName); nonMatchNames.add(fileName); return; } int itemIndex = itemIndex(fileName); int vsIndex = vsIndex(fileName); //itemIndex should >=6, vsIndex should >=11, eg. 1999 X MS A VS B.avi if (itemIndex < 6 || vsIndex < 11) Log.prt("*** " + file.getAbsolutePath()); else { //String matchName = getMatchName(fileName); matchNames.add(new MatchInfo(file.getAbsolutePath(), fileName)); } } /** Valid match file name: (2001) China (MS) Alice (VS) Bob(.)rmvb */ static boolean isValidMatchFile(String fileName) { if (fileName.endsWith(".txt")) return false; try { //If is a full file Name that contains path if (fileName.contains("\\")) { fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); } String year = fileName.substring(0, 4); Integer.parseInt(year); } catch (Exception e) { //Log.prt("*** " + fileName); return false; } return true; } static void readIndexFromFile(String indexFileName, List matchNames) throws FileNotFoundException { File file = new File(indexFileName); Scanner in = new Scanner(new FileInputStream(file)); //int lineNumber = 0; while (in.hasNextLine()) { //lineNumber++; String absolutePath = in.nextLine(); if (!isValidMatchFile(absolutePath)) { //Log.prt("*** " + absolutePath); continue; } //If is a full file Name that contains path String fileName = absolutePath; if (absolutePath.contains("\\")) { fileName = absolutePath.substring(absolutePath.lastIndexOf("\\") + 1); } int itemIndex = itemIndex(fileName); int vsIndex = vsIndex(fileName); //itemIndex should >=6, vsIndex should >=11, eg. 1999 X MS A VS B.avi if (itemIndex < 6 || vsIndex < 11) Log.prt("*** " + file.getAbsolutePath()); else { MatchInfo matchInfo = new MatchInfo(absolutePath, fileName); getMatchInfo(matchInfo); matchNames.add(matchInfo); } } in.close(); } public static void getMatchInfo(MatchInfo matchInfo) { //String s = "1997 All England MD Kang kyun-Ha tae Kwon VS Michael Sogaard-Jon holst.flv"; String fileName = matchInfo.fileName; //1) Year String year = fileName.substring(0, 4), match = "*", item = "*"; matchInfo.year = year; //2) Match int index = itemIndex(fileName); if (index > -1) { match = fileName.substring(5, index).trim(); matchInfo.match = match; } //3) Item if (index > -1) { item = fileName.substring(index + 1, index + 3); matchInfo.item = item; } //4) Players int vsIndex = vsIndex(fileName); try { if (vsIndex > -1) { matchInfo.player1 = fileName.substring(index + 4, vsIndex).trim(); int lastPoint = fileName.lastIndexOf("."); if (lastPoint > 0) matchInfo.player2 = fileName.substring(vsIndex + 4, lastPoint); } } catch (Exception e) { Log.error(e); Log.prt(fileName); } //Log.prt("%s, %s, %s, %s, VS, %s\n", year, match, item, a, b); //String ss = String.format("%s, %s, %s, %s, VS, %s", year, match, item, a, b); } public static int vsIndex(String source) { source = source.toUpperCase(); int index = source.indexOf(" VS "); return index; } public static int itemIndex(String source) { source = source.toUpperCase(); int index = source.indexOf(" MS "); if (index < 0) index = source.indexOf(" WS "); if (index < 0) index = source.indexOf(" MD "); if (index < 0) index = source.indexOf(" WD "); if (index < 0) index = source.indexOf(" XD "); return index; } } [/code] [code="java"]public class MatchInfo implements Comparable { public String absolutePath; public String fileName; public boolean isMatch = true; public String year; public String match; public String item; public String player1; public String player2; public String description; public MatchInfo(String absolutePath, String fileName) { super(); this.absolutePath = absolutePath; this.fileName = fileName; } public MatchInfo(String absolutePath, String fileName, boolean isMatch) { super(); this.absolutePath = absolutePath; this.fileName = fileName; this.isMatch = isMatch; } public MatchInfo(String absolutePath, String fileName, boolean isMatch, String year, String match, String item, String player1, String player2, String description) { super(); this.absolutePath = absolutePath; this.fileName = fileName; this.isMatch = isMatch; this.year = year; this.match = match; this.item = item; this.player1 = player1; this.player2 = player2; this.description = description; } @Override public String toString() { return fileName; } @Override public int compareTo(MatchInfo o) { return this.fileName.compareTo(o.fileName); } }[/code] [code="java"]public class MatchInfoCompator implements Comparator { @Override public int compare(MatchInfo o1, MatchInfo o2) { int x = BaseUtils.compareTo(o1.year, o2.year); if (x == 0) x = BaseUtils.compareTo(o1.match, o2.match); if (x == 0) x = BaseUtils.compareTo(o1.item, o2.item); if (x == 0) x = BaseUtils.compareTo(o1.player1, o2.player1); if (x == 0) x = BaseUtils.compareTo(o1.player2, o2.player2); return x; } }[/code]