file deletion in a directory with some conditions .

Discussion in 'C Programming' started by aki, Jul 31, 2008.

  1. aki

    aki Guest

    Hi All,

    I describe the problem as below.

    A directory( path known) , contains 0 to any number of files .
    The file names are with following structure :

    OMCID_NETYPE_NENAME_NAMEOFTHEAPPLIEDFILE_APPLYDATE_trans.csv
    For example :

    4_TC_TC_48_NoSTConfiguration_1971-1-1_trans.csv

    where
    OMCID =4
    NETYPE= TC
    NENAME= TC_48
    NAMEOFTHEAPPLIEDFILE=NoSTConfiguration
    APPLYDATE=1971-1-1

    i have to perform delete operation on the file with matching three
    fields .
    OMCID , NETYPE, NENAME (All three known )

    Could somebody try to answer the problem . or tel how to proceed

    Regards
    Aki
    aki, Jul 31, 2008
    #1
    1. Advertising

  2. aki

    santosh Guest

    aki wrote:

    >
    > Hi All,
    >
    > I describe the problem as below.
    >
    > A directory( path known) , contains 0 to any number of files .
    > The file names are with following structure :
    >
    > OMCID_NETYPE_NENAME_NAMEOFTHEAPPLIEDFILE_APPLYDATE_trans.csv
    > For example :
    >
    > 4_TC_TC_48_NoSTConfiguration_1971-1-1_trans.csv
    >
    > where
    > OMCID =4
    > NETYPE= TC
    > NENAME= TC_48
    > NAMEOFTHEAPPLIEDFILE=NoSTConfiguration
    > APPLYDATE=1971-1-1
    >
    > i have to perform delete operation on the file with matching three
    > fields .
    > OMCID , NETYPE, NENAME (All three known )
    >
    > Could somebody try to answer the problem . or tel how to proceed


    Firstly C doesn't have *any* support for directories, so you may want to
    ask in a group for your system like comp.unix.programmer.

    Secondly, once you get a list of all the files in your directory, you
    need to match the OMCID, NETYPE, and NENAME fields of these names with
    your criteria. If you don't want to write code to do this, you can use
    a regular expression library, which is not a part of Standard C but is
    nevertheless included with most major implementations. This will
    relieve you of the tedium of writing the string comparison code and the
    possibilities of bugs, testing, etc. A further advantage is that using
    this method you can easily adapt your code to match any criteria with
    just a few simple changes. Custom code may need major additions and
    restructuring.

    Another possibility, if your program will be restricted to suitable
    systems, is to use the system's delete file command. The Unix command
    for this, 'rm' has built-in support for file globbing and pattern
    matching. For further details ask in a group for your system.

    In summary while this can be easily done in fully portable C (provided
    the list of file names is somehow gathered, since ISO C has no support
    for reading directories), a more robust solution would be to use a
    pattern matching library. If you are on a POSIX system investigate
    regex.h and glob.h.
    santosh, Jul 31, 2008
    #2
    1. Advertising

  3. aki

    Ajay Guest

    On Jul 31, 6:10 am, santosh <> wrote:
    > aki wrote:
    >
    > > Hi All,

    >
    > >   I describe the problem as below.

    >
    > > A directory( path known) , contains  0 to any number of files .
    > > The file names are  with following structure :

    >
    > > OMCID_NETYPE_NENAME_NAMEOFTHEAPPLIEDFILE_APPLYDATE_trans.csv
    > > For example :

    >
    > > 4_TC_TC_48_NoSTConfiguration_1971-1-1_trans.csv

    >
    > > where
    > >  OMCID =4
    > > NETYPE= TC
    > > NENAME= TC_48
    > > NAMEOFTHEAPPLIEDFILE=NoSTConfiguration
    > > APPLYDATE=1971-1-1

    >
    > > i have to perform delete operation on the file with matching  three
    > > fields .
    > > OMCID , NETYPE, NENAME (All three known )

    >
    > > Could somebody try to answer the problem . or tel how to proceed

    >
    > Firstly C doesn't have *any* support for directories, so you may want to
    > ask in a group for your system like comp.unix.programmer.
    >
    > Secondly, once you get a list of all the files in your directory, you
    > need to match the OMCID, NETYPE, and NENAME fields of these names with
    > your criteria. If you don't want to write code to do this, you can use
    > a regular expression library, which is not a part of Standard C but is
    > nevertheless included with most major implementations. This will
    > relieve you of the tedium of writing the string comparison code and the
    > possibilities of bugs, testing, etc. A further advantage is that using
    > this method you can easily adapt your code to match any criteria with
    > just a few simple changes. Custom code may need major additions and
    > restructuring.
    >
    > Another possibility, if your program will be restricted to suitable
    > systems, is to use the system's delete file command. The Unix command
    > for this, 'rm' has built-in support for file globbing and pattern
    > matching. For further details ask in a group for your system.
    >
    > In summary while this can be easily done in fully portable C (provided
    > the list of file names is somehow gathered, since ISO C has no support
    > for reading directories), a more robust solution would be to use a
    > pattern matching library. If you are on a POSIX system investigate
    > regex.h and glob.h.


    Adding to what Santosh told:
    If you are on a Linux box and use are using Libc4 or Libc5, you can
    use functions scandir() and alphasort()
    to obtain directory listing. you need to look for header files
    dirent.h

    I am not sure about its portability to other systems :(

    For more information,

    http://www.gnu.org/software/libtool...ctory-Content.html#Scanning-Directory-Content
    Ajay, Jul 31, 2008
    #3
  4. aki

    santosh Guest

    Ajay wrote:
    > On Jul 31, 6:10 am, santosh <> wrote:
    >> aki wrote:
    >>
    >> > Hi All,

    >>
    >> > I describe the problem as below.

    >>
    >> > A directory( path known) , contains  0 to any number of files .
    >> > The file names are  with following structure :

    >>
    >> > OMCID_NETYPE_NENAME_NAMEOFTHEAPPLIEDFILE_APPLYDATE_trans.csv
    >> > For example :

    >>
    >> > 4_TC_TC_48_NoSTConfiguration_1971-1-1_trans.csv

    >>
    >> > where
    >> > OMCID =4
    >> > NETYPE= TC
    >> > NENAME= TC_48
    >> > NAMEOFTHEAPPLIEDFILE=NoSTConfiguration
    >> > APPLYDATE=1971-1-1

    >>
    >> > i have to perform delete operation on the file with matching  three
    >> > fields .
    >> > OMCID , NETYPE, NENAME (All three known )

    >>
    >> > Could somebody try to answer the problem . or tel how to proceed


    [ ... ]

    > Adding to what Santosh told:
    > If you are on a Linux box and use are using Libc4 or Libc5, you can
    > use functions scandir() and alphasort()
    > to obtain directory listing. you need to look for header files
    > dirent.h
    >
    > I am not sure about its portability to other systems :(


    They are not very portable at all. They are also deprecated by at least
    one standard. A more portable solution is to use
    opendir/readdir/closedir along with the facilities defined in regex.h.
    All of these are standardised by POSIX and hence fairly portable.

    For further discussions of these functions the OP should consider
    posting on comp.unix.programmer, where there is a higher likelyhood of
    receiving better, more accurate, peer reviewed answers, as there are
    far more active Unix/POSIX experts there than here.

    <snip>
    santosh, Jul 31, 2008
    #4
  5. santosh <> writes:
    [...]
    > Another possibility, if your program will be restricted to suitable
    > systems, is to use the system's delete file command. The Unix command
    > for this, 'rm' has built-in support for file globbing and pattern
    > matching. For further details ask in a group for your system.

    [...]

    The remove() function is standard in ISO C.

    <OT>No, Unix 'rm' doesn't have built-in support for file globbing and
    pattern matching; that's handled by the shell.</OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 3, 2008
    #5
  6. On 2 Aug 2008 at 23:32, Keith Thompson wrote:
    > santosh <> writes:
    >> Another possibility, if your program will be restricted to suitable
    >> systems, is to use the system's delete file command. The Unix command
    >> for this, 'rm' has built-in support for file globbing and pattern
    >> matching. For further details ask in a group for your system.

    >
    > No, Unix 'rm' doesn't have built-in support for file globbing and
    > pattern matching; that's handled by the shell.


    But system() goes via /bin/sh, so the effect is the same.

    If the user is using fork()/exec() directly, then again he can use
    /bin/sh -c to execute rm and get globbing.
    Antoninus Twink, Aug 3, 2008
    #6
  7. Antoninus Twink <> writes:
    > On 2 Aug 2008 at 23:32, Keith Thompson wrote:
    >> santosh <> writes:
    >>> Another possibility, if your program will be restricted to suitable
    >>> systems, is to use the system's delete file command. The Unix command
    >>> for this, 'rm' has built-in support for file globbing and pattern
    >>> matching. For further details ask in a group for your system.

    >>
    >> No, Unix 'rm' doesn't have built-in support for file globbing and
    >> pattern matching; that's handled by the shell.

    >

    [snip information more appropriate for a Unix group]

    I don't normally respond to AT, but in this case he has quoted me in a
    deliberately misleading manner. I had surrounded my remarks about the
    'rm' command with "<OT>" and "</OT"> tags. AT deleted those tags,
    giving the false impression that I care as little about topicality as
    he does.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 3, 2008
    #7
  8. In article <>,
    Keith Thompson <> wrote:
    >Antoninus Twink <> writes:
    >> On 2 Aug 2008 at 23:32, Keith Thompson wrote:
    >>> santosh <> writes:
    >>>> Another possibility, if your program will be restricted to suitable
    >>>> systems, is to use the system's delete file command. The Unix command
    >>>> for this, 'rm' has built-in support for file globbing and pattern
    >>>> matching. For further details ask in a group for your system.
    >>>
    >>> No, Unix 'rm' doesn't have built-in support for file globbing and
    >>> pattern matching; that's handled by the shell.

    >>

    >[snip information more appropriate for a Unix group]
    >
    >I don't normally respond to AT, but in this case he has quoted me in a
    >deliberately misleading manner. I had surrounded my remarks about the
    >'rm' command with "<OT>" and "</OT"> tags. AT deleted those tags,
    >giving the false impression that I care as little about topicality as
    >he does.


    God almighty, we couldn't have that, now could we?

    Geez, what small lives you people live.
    Kenny McCormack, Aug 4, 2008
    #8
  9. In article <>,
    Antoninus Twink <> wrote:
    >On 2 Aug 2008 at 23:32, Keith Thompson wrote:
    >> santosh <> writes:
    >>> Another possibility, if your program will be restricted to suitable
    >>> systems, is to use the system's delete file command. The Unix command
    >>> for this, 'rm' has built-in support for file globbing and pattern
    >>> matching. For further details ask in a group for your system.

    >>
    >> No, Unix 'rm' doesn't have built-in support for file globbing and
    >> pattern matching; that's handled by the shell.

    >
    >But system() goes via /bin/sh, so the effect is the same.
    >
    >If the user is using fork()/exec() directly, then again he can use
    >/bin/sh -c to execute rm and get globbing.


    Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    As far as the standard is concerned, system() could go via /keith/is/god.
    Kenny McCormack, Aug 4, 2008
    #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. Mark
    Replies:
    1
    Views:
    324
    Rodrigo Zechin
    Dec 8, 2005
  2. mscir
    Replies:
    4
    Views:
    446
    mscir
    Oct 12, 2004
  3. Tony
    Replies:
    7
    Views:
    334
  4. perseus
    Replies:
    0
    Views:
    321
    perseus
    Aug 12, 2003
  5. aki
    Replies:
    10
    Views:
    470
Loading...

Share This Page