diff --git a/DirectoryStatistics.java b/DirectoryStatistics.java new file mode 100644 index 0000000..6c5214b --- /dev/null +++ b/DirectoryStatistics.java @@ -0,0 +1,25 @@ +/* + * file DirectoryStatistics.java + * M. MAURICARD Francois-Xavier Pascal - 16/03/2006 + */ + + public class DirectoryStatistics extends Object +{ + // Attributes. + public int file_count; + public long directory_size; + + // Constructors. + public DirectoryStatistics() + { + this.file_count = 0; + this.directory_size = 0; + } + + // Instance methods. + public void add(DirectoryStatistics ds) + { + this.file_count += ds.file_count; + this.directory_size += ds.directory_size; + } +} diff --git a/ListMP3 all-drives.bat b/ListMP3 all-drives.bat new file mode 100644 index 0000000..09414dd --- /dev/null +++ b/ListMP3 all-drives.bat @@ -0,0 +1 @@ +@java ListMP3 A:\ B:\ C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ M:\ N:\ O:\ P:\ Q:\ R:\ S:\ T:\ U:\ V:\ W:\ X:\ Y:\ Z:\ \ No newline at end of file diff --git a/ListMP3.java b/ListMP3.java new file mode 100644 index 0000000..ed8e393 --- /dev/null +++ b/ListMP3.java @@ -0,0 +1,178 @@ +/* + * file ListMP3.java + * M. MAURICARD Francois-Xavier Pascal - 16/03/2006 + */ + +import java.io.File; +import java.io.FileWriter; +import java.lang.String; + +class ListMP3 +{ + public static void main (String[] args) + { + System.out.println("\nListMP3 v0.3_stable-20060316 by Francois-Xavier MAURICARD\n"); + + if (args.length == 0) + { + System.out.println("Error : Too less arguments.\nUsage : java ListMP3 [directory] ..."); + return; + } + + // Deleting the potential existing output file. + File output = new File("list.htm"); + if (output.exists()) + { + System.out.println("Deleting existing output file..."); + output.delete(); + } + + writeXHTMLHeader(output); + writeLine(output, "\t\t
\n"); + + DirectoryStatistics ds = new DirectoryStatistics(); + for (String s : args) + { + File directory = new File(s); + boolean error = false; + + // Test if directories passed in arguments exists and can be readed. + if (!directory.exists()) + { + System.out.println("Error : " + s + " does not exists."); + error = true; + } + else if (!directory.isDirectory()) + { + System.out.println("Error : " + s + " is not a directory."); + error = true; + } + else if (!directory.canRead()) + { + System.out.println("Error : " + s + " cannot be readed."); + error = true; + } + + if (!error) + { + System.out.println("Processing folder " + directory + "..."); + writeLine(output, "\t\t

" + directory + "

\n\t\t

\n"); + ds.add(processDirectory(directory, directory)); + writeLine(output, "\t\t

\n\t\t
\n"); + } + } + System.out.println("\n" + ds.file_count + " MP3s found for a total size of " + ds.directory_size / 1048576 + " MB."); + writeLine(output, "\t\t

" + ds.file_count + " MP3s found for a total size of " + ds.directory_size / 1048576 + " MB.

\n"); + writeXHTMLFooter(output); + } + + public static DirectoryStatistics processDirectory(File f, File parent) + { + DirectoryStatistics res = new DirectoryStatistics(); + + if (f.isDirectory()) + { + // List files of the current directory. + File[] files = f.listFiles(); + + // Test if the list is not null. + if (files != null && files.length != 0) + { + DirectoryStatistics ds = new DirectoryStatistics(); + boolean containsMP3 = false; + + // Search if the current directory contains a MP3. + for(int i = 0 ; i < files.length && !containsMP3 ; i++) + { + String filename = files[i].getName(); + + filename = filename.toLowerCase(); + containsMP3 = filename.endsWith(".mp3"); + } + // If it contains a MP3, we process it. + if (containsMP3) + { + // Managing output file. + File output = new File("list.htm"); + + for (File file : files) + { + ds.add(processDirectory(file, parent)); + } + + // Deletes if necessary the '\' at the beggining of the String defining the child path. + int root = 1; + if ((parent.toString().charAt(parent.toString().length() - 1) == '\\')) + { + root = 0; + } + + writeLine(output, "\t\t " + f.toString().substring(parent.toString().length() + root) + " - (" + ds.file_count + " tracks sizing " + ds.directory_size / 1048576 + " MB)
\n"); + res.add(ds); + } + // If it not contains a MP3, we treat it like a directory: recursion. + else + { + for (File file : files) + { + res.add(processDirectory(file, parent)); + } + } + } + } + + if (f.isFile()) + { + String filename = f.getName(); + + // Test if the file is a MP3 or not. + filename = filename.toLowerCase(); + if (filename.endsWith(".mp3")) + { + res.file_count++; + res.directory_size = f.length(); + // writeLine(new File("list.htm"), "\t\t " + f.getName() +"
\n"); + } + } + + return res; + } + + public static void writeXHTMLHeader(File f) + { + writeLine(f, "\n"); + writeLine(f, "\n"); + writeLine(f, "\n"); + writeLine(f, "\t\n"); + writeLine(f, "\t\tHTML document generated by ListMP3\n"); + writeLine(f, "\t\t\n"); + writeLine(f, "\t\n"); + writeLine(f, "\t\n"); + } + + public static void writeLine(File f, String s) + { + try + { + FileWriter fw = new FileWriter(f, true); + fw.write(s); + fw.close(); + } + catch (java.io.IOException e) + { + System.out.println("Error : Could not create/write output file."); + } + } + + public static void writeXHTMLFooter(File f) + { + writeLine(f, "\t\t
Generated by ListMP3 v0.3 created by Francois-Xavier MAURICARD
\n"); + writeLine(f, "\t\n"); + writeLine(f, "\n"); + } +}