'How can i count number of directories in current folder?

I want to count the number of directories in each folder it traverses. If a number of folders are equal to one, continue traversing, else select the current folder path.

I just want the path of the folder before it branches out.

I am editing this and including a pseudo-code of what I want.

IF     There are no .class files in the current directory
AND    Multiple Folders in the current directory
THEN   Use this folder path

More Edit:

How would I code this in Ant or Bash? I am opening this question to Bash too. I can execute this code through Ant.

Edit (12 May)

Right now I am using

<first>
    <fileset dir="${classes.dir}" includes="/*">
        <include name="**/*.class" />
    </fileset>
</first>

which gets me the first .class it searches in alphabetical order. Which I then get the path of by

<property name="classloc" value="${classes.dir}" relative="true" basedir="." />

This works fine when the folder structure of a project is like this:

Linear Folder Structure

But when folder structure is branched out and no .class is outside in the root folder then my code only get the path to one of the folders and all other folders are ignored. I need a path to the folder just before the .class files and their folders.

Branched out folders



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source