/* Java program to traverse the entire directory structure. */
/**
* This class traverses the file system, reporting on found files and directories.
* It starts with either the root directory or with the passed directory name.
*/
public class FileSystemTraverse {
/**
* This is a constant that holds the indentation character. It could be a blank,
* a period (the default), a hyphen, or any other character you choose.
*/
static final char indent_char = '.' ;
/**
* This is a class variable that is used to keep track of indentation levels.
*/
static int indent = 0 ;
/**
* This holds the string of characters that prefix each line of output.
*/
static String pad = "" ;
/**
* Holds the number of files found. Does not include files that require
* additional permission.
*/
static int num_files ;
/**
* Holds the number of directories found. Does not include directories that
* require additional permission.
*/
static int num_directories ;
/**
* The number of hidden files found.
*/
static int num_hidden ;
/**
* The main() method can be passed an optional argument. The argument, if passed,
* is expected to be a file name or directory name.
* <br />
* If an argument is not passed, a default directory name of "/" is assigned.
* <br />
* Then, routeObject() is passed to make a determination whether or not the
* argument is a file or directory.
*
* @param args The passed String - a file name or directory name
* @author Todd Burch
* @version 1.00 March 19, 2007
*
*/
public static void main(String[] args) {
/**
* Holds either the passed argument or a default value. This is the initial
* string the traversal process starts with.
*/
String dir ;
System.out.println("There are " + args.length + " args.") ;
if (args.length==0) {
dir = "/" ;
}
else dir = args[0] ;
System.out.println("Passed argument is ->" + dir + "<-") ;
FileSystemTraverse t = new FileSystemTraverse() ;
t.routeObject(dir) ;
System.out.println("There were " + t.num_files + " files (" + t.num_hidden +
" hidden) in " + t.num_directories + " directories.") ;
}
/**
* The routeObject() method accepts a string as input. The string is a full path name
* and is expected to represent a valid file name or a valid directory. A determination
* is made to whether the passed string represents a file or directory. If a directory,
* listFiles() is called to get a list of all the files. If it is a file, nothing is done.
* <br />
* A check is made to see if the passed string represents a symbolic (an alias). If it
* does, it is ignored and control is returned to the caller.
*/
private void routeObject(String thing) {
String tempPath = " " ;
this.doIndent(1) ;
java.io.File myFile = new java.io.File(thing) ;
if (myFile.isDirectory() ) {
try {
tempPath = myFile.getCanonicalPath() ; // reduce path/name to simplest form
}
catch (Exception e) {
System.out.println("*** Got Exception ***") ;
System.exit(8) ;
}
if ( tempPath.equalsIgnoreCase( myFile.getPath() ) ) {
num_directories++ ;
System.out.println(indent + ": " + pad + "Directory-> " + thing + " (#" + num_directories + ")") ;
this.listFiles(myFile) ;
}
}
this.doIndent(-1) ;
}
/**
* The doIndent() method control the indention of the report. It is passed a
* 1 or -1.
* <br />
* If a 1, the pad string is increased by one pad character. Otherwise, the pad
* string is reduced by one character.
*/
private void doIndent(int amount) {
indent = indent + amount ;
if (amount > 0 ) {
pad = pad + indent_char ;
}
else pad = pad.substring(1) ;
}
/**
* The listFiles() method is passed a File object that represents a directory.
* A list of all the files and directories contained therein are saved.
* <br />
* If a member is file, it's name is written to the report.
* If a member is a directory, the full directory name is saved to an array.
* Files with the hidden attribute are counted.
* <br />
* For each directory in the array, call routeObject().
*/
private void listFiles(java.io.File file) {
java.util.Vector<String> directories = new java.util.Vector<String>() ;
int i ;
String[] members = file.list() ; // get list of all files and directories
if (members==null) {
return ; // Might be null if permission denied
}
// Separate the files from the directories.
for (String s : members) {
java.io.File tempFile = new java.io.File(file.getPath() + java.io.File.separatorChar + s) ;
if ( tempFile.isDirectory() ) {
directories.add(s) ;
}
else {
num_files++ ;
System.out.println(indent+": " + pad + "File-> " + s + " (#" + num_files + ")") ;
if ( tempFile.isHidden() ) num_hidden++ ;
}
}
// Now, parse all the directories.
for (String s : directories) this.routeObject(file.getPath() + java.io.File.separator + s ) ;
}
}