import java.io.File; public class GenerateThumbnail { static final int MAX_DEPTH = 999; // Max 20 levels (directory nesting) static int count = 0; public static void scanFilesRecursively(File file, int depth) { try { String fileName = file.getName(); String thumbnailFullPath = ""; if (file.isDirectory()) { System.out.println(file.getPath()); } if (file.isDirectory() && depth < MAX_DEPTH) { if (fileName.equalsIgnoreCase("lr")) {//Check if the directory // is the one which has // large images //System.out.println(file.getName()); // Print name. if (!(fileName.equals("lr"))) {//Make the folder name to // lowercase if not already // in lowercase String originalFileName = file.getCanonicalPath(); String renamedFileName = originalFileName.substring(0, originalFileName.length() - 2) + "lr"; //System.out.println("Renamed filename : " + // renamedFileName); boolean success = file.renameTo(new File( renamedFileName)); //System.out.println("Rename : " + success); } //Delete the thumbnails which already exists File parent = file.getParentFile(); File[] files2Delete = parent.listFiles(); for (int i = 0; i < files2Delete.length; i++) { //System.out.println("FileName : " + // files2Delete[i].getName()); if (files2Delete[i].getName().substring( files2Delete[i].getName().lastIndexOf(".") + 1) .equalsIgnoreCase("jpg")) { files2Delete[i].delete(); } } } File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { scanFilesRecursively(files[i], depth + 1); } /* * Below code is jdk1.5 for (File f : file.listFiles()) { // Go * over each file/subdirectory. scanFilesRecursively(f, * depth+1); } */ //System.out.println("."); } else { String parent = file.getParentFile().getName(); String path = file.getPath(); String imageFilePath = file.getCanonicalPath(); //String fileName = file.getName(); if (parent.equalsIgnoreCase("lr") && (fileName.substring(fileName.lastIndexOf(".") + 1) .equalsIgnoreCase("jpg"))) { //Change the file name to lowercase String renamedFileName = imageFilePath.substring(0, imageFilePath.lastIndexOf("\\") + 1) + fileName.toLowerCase(); //System.out.println("renamedFileName : " + // renamedFileName); boolean success = file.renameTo(new File(renamedFileName)); //System.out.println("Rename : " + success); String grandparentName = file.getParentFile() .getParentFile().getName(); String grandparentNamePath = file.getParentFile() .getParentFile().getCanonicalPath();//getName(); String thumbnailFileName = fileName.substring(0, fileName.lastIndexOf(".")).toLowerCase() + "-t.jpg"; //System.out.println("thumbnailFileName : " + // thumbnailFileName); thumbnailFullPath = grandparentNamePath + "\\" + thumbnailFileName; //System.out.println(" fileName : " + fileName + " // thumbnailFullPath : " + thumbnailFullPath); // Print // name. Thumbnail.generateThumbnail(imageFilePath, thumbnailFullPath, 100, 75, 20, false); System.out.print("."); count++; } } } catch (Exception e) { System.out.println(e); } } public static void generate(String path) { File root = new File(path); if (root != null && root.isDirectory()) { scanFilesRecursively(root, 0); } else { System.out.println("Not a directory: " + root); } } public static void main(String[] args) { count = 0; // generate("29"); generate(args[0]); System.out.println("Number of thumbnails generated : " + count); System.out.println("Done."); /* * File root = new File("AD"); if (root != null && root.isDirectory()) { * scanFilesRecursively(root, 0); } else { System.out.println("Not a * directory: " + root); } */ } private String parseExtension(String imageURL) { StringBuffer stb = new StringBuffer(imageURL); for (int i = imageURL.length() - 1; i > 0; i--) { if (stb.charAt(i) == '.') { return imageURL.substring(i + 1, imageURL.length()); } } return "error"; } }