Re: file deletion in a directory with some conditions .
- From: Ajay <ajay.todimala@xxxxxxxxx>
- Date: Thu, 31 Jul 2008 11:10:03 -0700 (PDT)
On Jul 31, 6:10 am, santosh <santosh....@xxxxxxxxx> 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/manual/libc/Scanning-Directory-Content.html#Scanning-Directory-Content
.
- Follow-Ups:
- Re: file deletion in a directory with some conditions .
- From: santosh
- Re: file deletion in a directory with some conditions .
- References:
- Prev by Date: Re: Seg fault even though address space is accessible
- Next by Date: Re: file deletion in a directory with some conditions .
- Previous by thread: Re: file deletion in a directory with some conditions .
- Next by thread: Re: file deletion in a directory with some conditions .
- Index(es):
Relevant Pages
|