Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Check if a directory is empty and empty it

Reply
Thread Tools

Check if a directory is empty and empty it

 
 
Marcia Hon
Guest
Posts: n/a
 
      02-08-2004
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
 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      02-08-2004
"Marcia Hon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      02-08-2004
Marcia Hon <(E-Mail Removed)> scribbled the following:
> 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?


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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      02-08-2004
http://www.velocityreviews.com/forums/(E-Mail 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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
If you plan to shoplift, let us know.
Thanks
 
Reply With Quote
 
Marcia Hon
Guest
Posts: n/a
 
      02-08-2004
Thanks!!!

"Derk Gwen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail 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.
>
> --
> Derk Gwen http://derkgwen.250free.com/html/index.html
> If you plan to shoplift, let us know.
> Thanks



 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      02-08-2004
[snips]

On Sun, 08 Feb 2004 10:18:28 -0800, 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.


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


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-08-2004
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?


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


--
Martin Ambuhl
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-09-2004
Marcia Hon wrote:
>
> 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.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      02-14-2004
On Sun, 8 Feb 2004 18:35:49 -0000, "Peter Pichler" <(E-Mail Removed)>
wrote:
<snip>
> [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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.Delete Throws "the directory is not empty" codeboy ASP .Net 1 06-08-2009 12:08 PM
Recursive delete of directory - Directory not empty error Gordon Javascript 1 04-14-2008 06:28 PM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
DetailsView ASP Control and an empty field not an empty record. ButlerDJIAM ASP .Net 0 11-09-2006 06:40 PM



Advertisments