Directory Search

This class provides several methods to search for files and directories:

  • getFiles() - Returns a list of found in a directory
  • getSubDirectories() - Returns a list of found in a directory
  • getChildren() - Returns a list of both and found in a directory

Searching for Files

There are several different ways to search for files in a directory. Here are a few examples: directory = new"/temp");[] files;

   //Return a list of all files found in the current directory
     files = directory.getFiles();

   //Return a list of all files found in the current directory and in any subdirectories
     files = directory.getFiles(true);

   //Return a list of PDF documents found in the current directory and in any subdirectories
     files = directory.getFiles("*.pdf", true);

File Filters

In the last example, we performed a basic basic wildcard search for PDF documents (files that end with a .pdf extension). We did this my specifying a "*.pdf" file filter when we called getFiles(). You can also can pass in multiple wildcards using an String array. Here's an example of a resursive directory search using multiple wildcards.

     String[] filter = new String[]{"*.txt", "*.htm"};[] files = directory.getFiles(filter, true);

Of course, you can also use a standard Here's a simple example used to find txt files. filter = new {
        public boolean accept( file) {

            if (file.isHidden()) return false;
                if (file.isDirectory()) return true;
                else return (file.getName().endsWith(".txt"));
    };[] files = directory.getFiles(filter, true);

Efficient Search

In the previous examples, we used the getFiles() method to search for files. The getFiles() method returns an array of files. This is OK for small directories, but what if you want to search for tens of thousands of files? Waiting for the getFiles() method to return might take several minutes or even hours to complete and would require large amounts of memory. Instead, you can use the getChildren() method and process files and directories as soon as they are found.

Here's an example used to return all files and directories in a directory using the getChildren() method. In this example, we will perform a recursive directory search and process files and directories as soon as they are found.

  //Set flags
    boolean recursiveSearch = true;
    Object fileFilter = null;
    boolean wait = false;

  //Initiate search
    java.util.List results = directory.getChildren(recursiveSearch, fileFilter, wait);
    while (true){
        Object item;
        synchronized (results) {

          //Wait for files/directories to be added to the list
            while (results.isEmpty()) {
              try {
              catch (InterruptedException e) {

          //Grab the next available file/directory from the list
            item = results.remove(0);

      //Do something with the file/directory
        if (item!=null){
            if (item instanceof{
       file = ( item;
       dir = ( item;
        else{ //Item is null. This is our queue that the search is done!

Recursive Search

Recursive directory searches highlighted in the examples above are extremely fast. This is thanks, in large part, to the multi-threaded design of the class. Additional efficiencies are gained on Windows file systems by leveraging native Windows API (via the javaxt-core JNI). On Windows file systems, the directory search is approximately 2x faster than the listFiles() method on local file systems and over 10x faster searching network drives.

Additional Options for Windows

The class offers several additional capabilities specific for JVMs running on Windows. Among these is the ability to list all the shared drives found on a server. Example:
    String serverName = "\\\\Orion"; //<--Can also be an IP address like "\\"
    for ( share : new{
Another Windows-specific feature is the ability to list all the root/logical drives on the host machine, including disconnected network drives. This is something not available in the standard Java release ( does not return disconnected network drives on Windows).
    for ( drive :{