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

