Solved

Checking if directory is an empty directory

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

749 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