Solved

Checking if directory is an empty directory

Posted on 1998-01-14
5
188 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now