I am using Java to search for a file or folder that matches a specific case. For instance I want to search through all the files and folders and iterate through them as many times as necessary until I find a result.

### The catch

I am searching for files and folders through a binary image of a file system. This means I doubt there are any preexisting classes such `java.file.*` to do it for me. If it helps, I can tell you that I can tell the difference between files and folders easily enough. (a folder is flagged as 0x20)

The only way I can think of is to do a standard loop and iterate several times, but this limits me if a file or folder is several folders deep.

All input greatly welcome

### Edit

Just to clarify, I'm using a binary image and searching though it for directories.

I think the term "directory" could be misleading. Given an initial vaule of 0 I want to examine a series of bytes. If in the correct place I encounter a 0x20 (directory marker), I need to jump to a new location in the binary image and start searching again. If I encounter 0x20 again, I need to jump and start again; when finished I return to the point I was and continue to search for another 0x20. I need to do this until I have no more 0x20's in specific spots.

I can do this using *n* loops but that will only take me *n* directorys deep. I am wondering if anyone has any ideas on how to do this until there are no more directory markers.

Not really sure if this clarifies things but I hope it does.

---

EDIT
This is code that I'm using and im getting a stack overflow error. any ideas on how i can improve it?

```
  public boolean isDirectory(int dirAttr)
{
    if (dirAttr == dir.ATTR_DIRECTORY)
        return true;
    else return false;
}
public void searchDirectory(int clusterNum, String name)
{
    for (int i = 0; i<32;i++)
    {
        if(dir.DIR_NameS(clusterNum, i).contains(name))
        {
            System.out.println("Found a Match");
            System.out.println("File name = " + dir.DIR_NameS(clusterNum, i));
            System.out.println("File size in bytes = "+dir.DIR_FileSize(clusterNum, i));
            System.out.println("File starting cluster = "+dir.DIR_FstClusLO(clusterNum, i));
            System.out.println();
        }
        if(this.isDirectory(dir.DIR_Attr(clusterNum, i)))
        {
            searchDirectory(dir.DIR_FstClusLO(clusterNum, i), name);
        }
    }
}

```