file deletion in a directory with some conditions .

A

aki

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 how to proceed

Regards
Aki
 
M

Michael DOUBEZ

aki a écrit :
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
[snip]
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 how to proceed

Iterate over the elements of the directory and match the beginning of
the filename with "<OMCID>_<NETYPE>_<NENAME>" to locate and delete the
files.

Practically
* iterating in a directory with Boost:
http://www.boost.org/doc/libs/1_35_0/libs/filesystem/example/simple_ls.cpp
* for matching:
std::string filename="4_TC_TC_48";
if(it->leaf().compare(0,filename.size(),filename)==0)
{ //file to delete
}

Generally:
* You'd rather use
rm -f /path/4_TC_TC_48_*_trans.csv
DEL /F C:\path\4_TC_TC_48_*_trans.csv
* Show what you have done up to now before asking advices
 
I

Ioannis Gyftos

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 how to proceed

Regards
Aki

If you do not want to reinvent the wheel, on a UNIX system you could
use something like that...

#!/bin/bash
#(I haven't actually tested)
function_foo() {
ls | awk -F _ -v var1=$1 -v var2=$2 -v var3=$3 '$1 == var1 && $2 ==
var2 && $3 == var3 {print($0)}' | xargs rm
}
 
A

aki

aki a écrit :
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
[snip]
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 how to proceed

Iterate over the elements of the directory and match the beginning of
the filename with "<OMCID>_<NETYPE>_<NENAME>" to locate and delete the
files.

Practically
* iterating in a directory with Boost:http://www.boost.org/doc/libs/1_35_0/libs/filesystem/example/simple_l...
* for matching:
std::string filename="4_TC_TC_48";
if(it->leaf().compare(0,filename.size(),filename)==0)
{ //file to delete

}

Generally:
* You'd rather use
rm -f /path/4_TC_TC_48_*_trans.csv
DEL /F C:\path\4_TC_TC_48_*_trans.csv
* Show what you have done up to now before asking advices

thanks Michael for answering .
i feel i was short on providing all required information .
My apologies for that .
Well i want to write a C++ code for solving above problem under
solaris .
One way of doing which seems very normal is
*iterating in a directory for all files
* matching the file to be deletes
* perform delete operation on the file
as you told .
But from coding point of view ,
can i do it without sing boost .
Also can you elaborate , i am newbie in c++ ;)

thanks
Aki
 
M

Michael DOUBEZ

aki a écrit :
*iterating in a directory for all files
* matching the file to be deletes
* perform delete operation on the file
as you told .
But from coding point of view ,
can i do it without sing boost .

C++ language doesn't deal with filesystem issues.
You can use the dirent.h header (look for opendir and scandir functions).

Otherwise, you can still use
system("rm -f /path/4_TC_TC_48_*_trans.csv");
:)
 
A

aki

aki a écrit :


C++ language doesn't deal with filesystem issues.
You can use the dirent.h header (look for opendir and scandir functions).

Otherwise, you can still use
system("rm -f /path/4_TC_TC_48_*_trans.csv");
:)


Thanks for input.
Well for system command path cannot be a variable , i tested it.
as this will not work
system("rm -f /path/OMCID_NETYPE_NENAME_*_trans.csv");

because OMCID , NETYPE, NENAME are variables.

Well , i think scandir will work for my case .

my job will be done if i get a builtin unix function
which can do this operation
eg.
string1="4_TC_TC_48"
string2= "4_TC_TC_48_NoSTConfiguration_1971-1-1_trans.csv"
fun(string1 , string2);
return value should be 4_TC_TC_48 i.e. all consecutive matching
character
Any inputs please.
regards
Aki
 
J

James Kanze

On Aug 1, 12:23 pm, Michael DOUBEZ <[email protected]> wrote:

[...]
Well for system command path cannot be a variable,

Of course it can be. It can be an expression, just like any
other function argument.
i tested it. as this will not work
system("rm -f /path/OMCID_NETYPE_NENAME_*_trans.csv");
because OMCID , NETYPE, NENAME are variables.

So you need a different expression. Something like:

system( ("rm -f /path/" + OMCIDE + '_' + NETYPE + '_' +
NENAME + "_*_trans.swv").c_str() ) ;
 
A

aki

[...]

Well for system command path cannot be a variable,

Of course it can be. It can be an expression, just like any
other function argument.
i tested it. as this will not work
system("rm -f /path/OMCID_NETYPE_NENAME_*_trans.csv");
because OMCID , NETYPE, NENAME are variables.

So you need a different expression. Something like:

system( ("rm -f /path/" + OMCIDE + '_' + NETYPE + '_' +
NENAME + "_*_trans.swv").c_str() ) ;








Hello ,

i tried with this as following , but not working :(
string omcid;
string netype ;
string nename ;
//some stuff to get values inside the string variables

TrDebug<<"omcid"<<omcid<<endl; // here i printed the value it is
coming correct
TrDebug<<"netype"<<netype<<endl;// here also
TrDebug<<"nename"<<nename<<endl; // here also
system( ("rm -f /path/" + omcid + '_' + netype + '_' + nename +
"_*_trans.csv").c_str() ) ;


thanks
Aki
 
J

James Kanze

On Aug 1, 12:23 pm, Michael DOUBEZ <[email protected]> wrote:
[...]
Well for system command path cannot be a variable,
Of course it can be. It can be an expression, just like any
other function argument.
i tested it. as this will not work
system("rm -f /path/OMCID_NETYPE_NENAME_*_trans.csv");
because OMCID , NETYPE, NENAME are variables.
So you need a different expression. Something like:
system( ("rm -f /path/" + OMCIDE + '_' + NETYPE + '_' +
NENAME + "_*_trans.swv").c_str() ) ;
i tried with this as following , but not working :(
string omcid;
string netype ;
string nename ;
//some stuff to get values inside the string variables
TrDebug<<"omcid"<<omcid<<endl; // here i printed the value it is
coming correct
TrDebug<<"netype"<<netype<<endl;// here also
TrDebug<<"nename"<<nename<<endl; // here also
system( ("rm -f /path/" + omcid + '_' + netype + '_' + nename +
"_*_trans.csv").c_str() ) ;

And how is it not working? It should compile (as long as one of
the first two operands in the sequence of +'s is a string, which
seems to be the case). What does system() return? Otherwise:
try redirecting the output of rm (both standard out and standard
error) to a file, and see what it contains.
 
A

aki

[...]
Well for system command path cannot be a variable,
Of course it can be. It can be an expression, just like any
other function argument.
i tested it. as this will not work
system("rm -f /path/OMCID_NETYPE_NENAME_*_trans.csv");
because OMCID , NETYPE, NENAME are variables.
So you need a different expression. Something like:
system( ("rm -f /path/" + OMCIDE + '_' + NETYPE + '_' +
NENAME + "_*_trans.swv").c_str() ) ;
i tried with this as following , but not working :(
string omcid;
string netype ;
string nename ;
//some stuff to get values inside the string variables
TrDebug<<"omcid"<<omcid<<endl; // here i printed the value it is
coming correct
TrDebug<<"netype"<<netype<<endl;// here also
TrDebug<<"nename"<<nename<<endl; // here also
system( ("rm -f /path/" + omcid + '_' + netype + '_' + nename +
"_*_trans.csv").c_str() ) ;

And how is it not working? It should compile (as long as one of
the first two operands in the sequence of +'s is a string, which
seems to be the case). What does system() return? Otherwise:
try redirecting the output of rm (both standard out and standard
error) to a file, and see what it contains.

hii ,

i cross checked again ...
path was not correct ..
corrected and now its working fine...;)

Thanks all of u ...for your valuable feedback...

regards
aki
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top