Solved

Checking if directory is an empty directory

Posted on 1998-01-14
5
205 Views
Last Modified: 2010-04-02
How can I check if the given directory that exist is an empty directory in Unix C?
0
Comment
Question by:wombat032397
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1256990
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.


Note:

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

0
 
LVL 11

Expert Comment

by:alexo
ID: 1256991
a "D" grade?
Gee, thanx...

0
 

Author Comment

by:wombat032397
ID: 1256992
I thought "D" would be nice since I solve the problem without using the 2 suggestion you have given.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1256993
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.
0
 

Author Comment

by:wombat032397
ID: 1256994
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.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question