My recursive file search is stuck in loop...
I am running on OS X using Java version 1.4.2_09.
I am trying to make a recursive search through the file system to count up all the objects that qualify as files...and eventually perform some function on them that is not yet written. My goal is to create the same output that the unix command: "find / -type file" would return. However, my search is getting stuck in an endless loop which gets deeper and deeper and deeper... I think the cause is the search following links, or mount points.
Here are selected lines of output showing the repetition created by the scan. There are tons of files and folders between each of these lines, but the key is that the same Application file path (in italics) is being touch multiple times, and the prefix (underlined path) is being prepended to the to itself over and over and over again.
First Time Pass: (this is correct - and in italics)
/Applications/Adium.app/Contents/Resources/is.lproj
Second Time Pass: (notice the italics, we covered this path is pass 1 - notice underlined)
/automount/Servers/powerbook.local/Applications/Adium.app/Contents/Resources/is.lproj
Third Time Pass: (notice the underlined, it is duplicated and also seen in pass 2)
/automount/Servers/powerbook.local/automount/Servers/powerbook.local/Applications/Adium.app/Contents/Resources/is.lproj
N Time Pass:
will just repeat - "/automount/Servers/powerbook.local" N-1 times.
--
I can't get past the automount directory...making me believe that it is just following mount points. The funny part is, if you navigate one of these rediculously long (and repetative) paths that is outputed, it is legitament and takes you to the file. Here is the code minus the methods to verify user input.
import java.lang.*;
import java.io.*;
import java.util.*;
publicclass hogan{
public String src=null;//source...where to begin recursive scan
public File[] fnfArray=null;
public List fnfList;
public ArrayList list, deeperList;
public Iterator filesIter;
publicint folders=0, files=0, unknown=0;
publicstaticvoid main (String args[]){
hogan h=new hogan();//this is because I made my variable public. is that bad or good?
h.vInput(args);//verifies the switches are formated correctly in another method.
h.vSwitches(new File(h.src));//verifies the values of the switches are legitament.
h.getFiles(new File(h.src));//begin recursive scan.
System.out.println("\nTotal Files Found:" + h.files);
System.out.println("Total Folders Found: " + h.folders);
System.out.println("Total Unknown Found: " + h.unknown);
System.out.println("Total Objects Found: " + (h.files+h.folders+h.unknown) +"\n");
}
public ArrayList getFiles(File startDir)
{
list=new ArrayList();
fnfArray=startDir.listFiles();//"Files And Folders Array"
fnfList=Arrays.asList(fnfArray);//Files And Folders List"
Iterator filesIter=fnfList.iterator();
File currentFile=null;
while(filesIter.hasNext()){
currentFile=(File)filesIter.next();
list.add(currentFile);
if((currentFile.isDirectory()) && (currentFile.listFiles()!=null)){
System.out.println("Folder: " + currentFile);//output where we are in the scan
deeperList=getFiles(currentFile);
list.addAll(deeperList);
}
elseif(currentFile.isFile()){
System.out.println("File:" + currentFile);//output where we are in the scan
files++;
}
}
Collections.sort(list);
return list;
}
}
-
Is there a way to prevent a recursive search from following links or mount points. Can you set some attribute to files touched that says..."You've been here already, move on to the next."? Thanks alot, and I'm sorry for my code, I am as green as it gets when it comes to java, and as rusty as it gets when it comes to programming. Thanks!!!
Hippie Joe

