Check if a directory is empty and empty it

Discussion in 'C Programming' started by Marcia Hon, Feb 8, 2004.

  1. Marcia Hon

    Marcia Hon Guest

    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
     
    Marcia Hon, Feb 8, 2004
    #1
    1. Advertising

  2. "Marcia Hon" <> wrote in message
    news:...
    > 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
     
    Peter Pichler, Feb 8, 2004
    #2
    1. Advertising

  3. Marcia Hon <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
    - Bob Wiley
     
    Joona I Palaste, Feb 8, 2004
    #3
  4. Marcia Hon

    Derk Gwen Guest

    (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
     
    Derk Gwen, Feb 8, 2004
    #4
  5. Marcia Hon

    Marcia Hon Guest

    Thanks!!!

    "Derk Gwen" <> wrote in message
    news:...
    > (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
     
    Marcia Hon, Feb 8, 2004
    #5
  6. [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.
     
    Kelsey Bjarnason, Feb 8, 2004
    #6
  7. 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
     
    Martin Ambuhl, Feb 8, 2004
    #7
  8. Marcia Hon

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Feb 9, 2004
    #8
  9. On Sun, 8 Feb 2004 18:35:49 -0000, "Peter Pichler" <>
    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
     
    Dave Thompson, Feb 14, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Hedlund
    Replies:
    18
    Views:
    654
    Alex Martelli
    Sep 3, 2006
  2. Replies:
    5
    Views:
    985
    X-Centric
    Jun 30, 2005
  3. NotGuru
    Replies:
    3
    Views:
    489
    NotGuru
    Mar 24, 2008
  4. codeboy
    Replies:
    1
    Views:
    1,598
    Juan T. Llibre
    Jun 8, 2009
  5. Gordon
    Replies:
    1
    Views:
    237
    nolo contendere
    Apr 14, 2008
Loading...

Share This Page