Check if a directory is empty and empty it

M

Marcia Hon

Hi,

I would like to know how the following may be accomplished in C:

1. Check if a directory is empty.
2. Empty the directory.

Please if you know how could you state what statements in C would accomplish this?

Thanks,
Marcia Hon
 
P

Peter Pichler

Marcia Hon said:
I would like to know how the following may be accomplished in C:

1. Check if a directory is empty.
2. Empty the directory.

Please if you know how could you state what statements in C would
accomplish this?

Marcia,

Please be aware that this newsgroup discusses the C language as defined by
the ISO standard, sometimes also refered to as ANSI C. In standard C, there
are no such things like directories. True, there are files in C, but to do
anything with them, you must know their names, so standard C is not good for
your purpose. You will need to use some language extension for your
platform. These may be different depending on whether you want to program
for DOS, Windows, OS/2, Open VM, Macintosh, Linux, Unix, DeathStation 9000
OS (in increasing order of preference).

Please consult the manual coming with your implementation or ask in a
newsgroup relevant to your compiler and/or operating system.

HTH,

Peter
 
J

Joona I Palaste

Marcia Hon said:
I would like to know how the following may be accomplished in C:
1. Check if a directory is empty.
2. Empty the directory.
Please if you know how could you state what statements in C would accomplish this?

None. ISO standard C provides no direct support for directories at all.
You'll have to ask in a newsgroup dedicated to your own implementation.
 
D

Derk Gwen

(e-mail address removed) (Marcia Hon) wrote:
# Hi,
#
# I would like to know how the following may be accomplished in C:
#
# 1. Check if a directory is empty.
# 2. Empty the directory.
#
# Please if you know how could you state what statements in C would accomplish this?

If you want to unconditionally destroy a directory, then you can do something
like

static int deleteDirectory(char *path) {
#ifdef __some-define-name-that-is-only-defined-for-unix__
char deleteFormat = "rm -rf '%s'";
#endif
#ifdef __some-define-name-that-is-only-defined-for-windows__
char deleteFormat = "the windows equivalent of rm -rf '%s'";
#endif
#ifdef etc
...
#endif
char *command = malloc(strlen(path)+strlen(deleteFormat)+1);
int rc;
sprintf(command,deleteFormat,path);
rc = system(command);
free(command);
return rc;
}

Otherwise, you will need some system specific interface to examine a
directory contents, define what 'empty' means, and a system specific
way to delete files and directories.
 
K

Kelsey Bjarnason

[snips]

Hi,

I would like to know how the following may be accomplished in C:

1. Check if a directory is empty.
2. Empty the directory.

In a word, no. C has no support for directoreies. Your implementation
might, but that'd make it off-topic in these parts.
 
M

Martin Ambuhl

Marcia said:
Hi,

I would like to know how the following may be accomplished in C:

1. Check if a directory is empty.
2. Empty the directory.

Please if you know how could you state what statements in C would accomplish this?

Those that your implementation provides for these OS-specific concepts.
 
C

CBFalconer

Marcia said:
Thanks!!!
WARNING: You are getting what you paid for, an off-topic answer to
an off-topic question with no assurance that it is anywhere near
appropriate for you. People who give these off-topic answers are
even more guilty than you, because they should know better.

At any rate, go to a newsgroup with at least your operating
systems name in it, and ask again.

q: How do I get a kitty?
a: Go out in the woods and look for a black furry animal with a
white stripe and a fluffy tail. Pick it up by the tail.
 
D

Dave Thompson

[directories] may be different depending on whether you want to program
for DOS, Windows, OS/2, Open VM, Macintosh, Linux, Unix, DeathStation 9000
OS (in increasing order of preference).
Is there really an Open VM, or do you mean OpenVMS? And you might
want to separate classic MacOS and MacOS X -- probably in that order.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top