ericjv
ericjv

Reputation: 31

Is there any way I can significantly improve the efficiency of this file search?

This method uses recursion to find a file with a certain string in its name. It's searching on a network drive and sometimes has to search through hundreds or even thousands of directories before it can find what it's looking for. It's rather slow--sometimes taking 5-10 seconds. I doubt that the delay is caused by the network connection as this network is very fast for everything else. Anyway, it's just something I whipped up so there's probably something more efficient out there.

public static File findFile(File root, String name)
{
    File [] dir = root.listFiles();
    File a = null;

    for(int i = 0; i < dir.length; i++)
    {
        if(dir[i].isDirectory() && a == null)
            a = findFile(dir[i],name);
        else if(dir[i].getName().indexOf(name) > -1)
            return dir[i];
    }

    return a;
}

So is there any way to improve this? Or is the process of searching that many directories just pretty much always going to be that slow? Thanks.

Upvotes: 0

Views: 112

Answers (2)

Kyle
Kyle

Reputation: 4238

This may be overkill, but consider building an index.

Personally I've used Apache Lucene in the past to index all sorts of web assets, including image files, PDFs, html, etc. With Lucene 4.1, adding to the index as well as searching the index is incredibly simple.

Add a folder (and all subfolders) to the index

To start, it's pretty simple to add everything in your networked drive to the index:

java org.apache.lucene.demo.IndexFiles -docs {path-to-folder}

Search the index

Once you've added all files necessary to your index, you can use the search indexer provided by the Lucene library to search for documents:

IndexSearcher.search(query, n)

There are endless possibilities of configuration, and you are not limited to merely searching filenames either.

Upvotes: 1

rburny
rburny

Reputation: 1569

In a large directory structure, searching has to take longer. If you need speed, consider index-based approach (construct and dynamically update an index of file names, and only do lookups in it).

This is exactly how Linux locate command works, and its searches are immediate.

Upvotes: 0

Related Questions