0

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.

ericjv
  • 31
  • 1
  • 3
  • 2
    Five to 10 seconds is pretty good. I've seen Windows Explorer searches that take much longer than that. – Robert Harvey Jan 23 '13 at 20:55
  • There's an obvious problem: search doesn't stop even if file is found at once. – Andrew Logvinov Jan 23 '13 at 20:57
  • Is the root always the same when findFile gets called outside the recursion? – dchhetri Jan 23 '13 at 20:57
  • @Andrew - that's true. I suppose if I added else if(a != null) return a; into the for loop that could make it faster? to user814628 - Yes – ericjv Jan 23 '13 at 21:00
  • I'd rather stick with a boolean variable that is set when file is found and checked before invoking `findFile()`. You could also parallelize the search but I can't tell how much you'd gain with this. As @RobertHarvey mentioned, 5-10 seconds is pretty good. – Andrew Logvinov Jan 23 '13 at 21:04
  • I guess 5-10 seconds is alright. Searching with Windows Explorer actually did take minutes. I figured that was just the Windows XP search being sluggish (Yes I am unfortunately forced to use Windows XP on this machine at the moment). I'll take your suggestions into consideration. Thanks for the help. – ericjv Jan 23 '13 at 21:16

2 Answers2

1

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.

Kyle
  • 4,202
  • 1
  • 33
  • 41
  • This is not something I'm familiar with at all but I will check it out when I get a chance. Thanks. – ericjv Jan 23 '13 at 21:17
0

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.

rburny
  • 1,559
  • 1
  • 9
  • 28