Problems with search file method

Hi everyone!! I'm trying to write a method that search for a file in the PC, recursively... But it just don't works!! :(

Here's the code (it's pretty short) and then I'll make some comments:

// By default the file has not been found in the begginning of the execution

static String result ="File not found!";

publicstaticvoid searchFile(File dir, String name){

if (dir.isDirectory()){

String[] children = dir.list();

if(children !=null){

for (int i = 0; i < children.length; i++){

if (children[i].equals(name)){

result = dir.getPath();

break;

}

else

searchFile(new File(dir, children[i]), name);

}

}

}

elseif (dir.getName().equals(name)) result = dir.getPath();

}

Well, the basic idea is the following: before calling the method, I create a new file with the name of the root directory (At Linux this is "\", and at Windows this is "C:\").

The root file represents a directory, so it enters the initial condition and creates a list with all the root's objects (files and directories).

Then it verifies if any element of this list is the searched file... If it's not, the method is called recursively.

In the recursion, if we have a directory, it searches for all his items again... If this object is a file, it just verifies if the name is equal to the searched file name.

The answer is recorded in a static String, external to the method, so that I don't loose the answer at the recursion stack. This answer is the directory name of the searched file.

Well... This is my method, but it just don't work, and ALWAYS returns "File not found!" :cry:

Someone knows what's wrong with my code? I just don't know what to do anymore...

Thanks from now for any help and best regards for you all!! :D

[2694 byte] By [Rhaigera] at [2007-11-27 7:43:22]
# 1
if (children[i].equals(name)){should be replaced withif (children[i].getName().equals(name)) {
calvino_inda at 2007-7-12 19:24:10 > top of Java-index,Java Essentials,Java Programming...
# 2
I dont know about you but your code works fine for me without any modifications. Are you sure that you are not overshadowing the result variable somewhere.
DarumAa at 2007-7-12 19:24:10 > top of Java-index,Java Essentials,Java Programming...
# 3
As an aside, if you are searching for multiple files with the same name located in different folders then your algo is fine but if you are just searching for the first instance of a file name then you might want to consider putting a return condition at the beginning of your method.
DarumAa at 2007-7-12 19:24:10 > top of Java-index,Java Essentials,Java Programming...
# 4
notice: sorry for my previous answers, i thought you had used "listFiles" X)
calvino_inda at 2007-7-12 19:24:10 > top of Java-index,Java Essentials,Java Programming...