Checking if directory is an empty directory

How can I check if the given directory that exist is an empty directory in Unix C?
Who is Participating?
alexoConnect With a Mentor Commented:
Suggestion #1:

Try to remove it.  If you get an ENOTEMPTY error, it is not empty.
(Don't forget to recreate it afterwards).

Suggestion #2:

Do a findfirst/findnext loop on the directory using a "*" filename.  Skip over the "." and suchlike entries.


No matter what approach you choose, keep in mind that access permissions can influence the result.

a "D" grade?
Gee, thanx...

wombat032397Author Commented:
I thought "D" would be nice since I solve the problem without using the 2 suggestion you have given.
How did you solve it then?

Incidently, "D" means "shitty answer".  I don't think it was such a bad suggestion.  I'd prefer to get the answer rejected altogether instead of having it be proudly(?) displayed on my info.  It's bad enough that questions that are not graded for a long time automatically expire with a "C" grade...

Do you know that you can ask the X.X. admins to cancel the question and refund the points?  A much better option for all the parties involved.
wombat032397Author Commented:
I thought "D" means acceptable, and I thought that maybe if I
gave this question a grade then by asking the admin staff to
remove off the question, other people whom may have similar
problem may read your suggestion and get the benefit.

I solve it by keeping a counter in the readdir()loop. Actually,
it is bad practise to try removing a directory just to find out
whether it is an empty directory or not.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.