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.