Solved

Checking if directory is an empty directory

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

777 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