file deletion in a directory with some conditions .

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aki

    file deletion in a directory with some conditions .

    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_NE NAME_NAMEOFTHEA PPLIEDFILE_APPL YDATE_trans.csv
    For example :

    4_TC_TC_48_NoST Configuration_1 971-1-1_trans.csv

    where
    OMCID =4
    NETYPE= TC
    NENAME= TC_48
    NAMEOFTHEAPPLIE DFILE=NoSTConfi guration
    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
  • Ian Collins

    #2
    Re: file deletion in a directory with some conditions .

    aki wrote:
    Hi All,
    >
    Please don't multi-post on Usenet. The answer is the same here is it
    was on c.l.c.

    --
    Ian Collins.

    Comment

    • Ioannis Gyftos

      #3
      Re: file deletion in a directory with some conditions .

      On Jul 31, 12:50 pm, aki <akhileshrawat. ..@gmail.comwro te:
      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_NE NAME_NAMEOFTHEA PPLIEDFILE_APPL YDATE_trans.csv
      For example :
      >
      4_TC_TC_48_NoST Configuration_1 971-1-1_trans.csv
      >
      where
       OMCID =4
      NETYPE= TC
      NENAME= TC_48
      NAMEOFTHEAPPLIE DFILE=NoSTConfi guration
      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
      }

      Comment

      • aki

        #4
        Re: file deletion in a directory with some conditions .

        On Jul 31, 4:48 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
        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_NE NAME_NAMEOFTHEA PPLIEDFILE_APPL YDATE_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>_<NETYP E>_<NENAME>" to locate and delete the
        files.
        >
        Practically
        * iterating in a directory with Boost:http://www.boost.org/doc/libs/1_35_0...ample/simple_l...
        * for matching:
        std::string filename="4_TC_ TC_48";
        if(it->leaf().compare (0,filename.siz e(),filename)== 0)
        { //file to delete
        >
        }
        >
        Generally:
        * You'd rather use
        rm -f /path/4_TC_TC_48_*_tr ans.csv
        DEL /F C:\path\4_TC_TC _48_*_trans.csv
        * Show what you have done up to now before asking advices
        >
        --
        Michael
        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





        Comment

        • aki

          #5
          Re: file deletion in a directory with some conditions .

          On Aug 1, 12:23 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
          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_*_tr ans.csv");
          :)
          >
          --
          Michael

          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_NE NAME_*_trans.cs v");

          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_T C_48"
          string2= "4_TC_TC_48_NoS TConfiguration_ 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






          Comment

          • James Kanze

            #6
            Re: file deletion in a directory with some conditions .

            On Aug 1, 6:20 pm, aki <akhileshrawat. ..@gmail.comwro te:
            On Aug 1, 12:23 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
            [...]
            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_NE NAME_*_trans.cs v");
            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() ) ;

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • aki

              #7
              Re: file deletion in a directory with some conditions .

              On Aug 1, 11:17 pm, James Kanze <james.ka...@gm ail.comwrote:
              On Aug 1, 6:20 pm, aki <akhileshrawat. ..@gmail.comwro te:
              >
              On Aug 1, 12:23 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
              >
              [...]
              >
              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_NE NAME_*_trans.cs v");
              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<<e ndl; // here i printed the value it is
              coming correct
              TrDebug<<"netyp e"<<netype<<end l;// here also
              TrDebug<<"nenam e"<<nename<<end l; // here also
              system( ("rm -f /path/" + omcid + '_' + netype + '_' + nename +
              "_*_trans.csv") .c_str() ) ;


              thanks
              Aki
              --
              James Kanze (GABI Software) email:james.ka. ..@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • James Kanze

                #8
                Re: file deletion in a directory with some conditions .

                On Aug 4, 2:07 pm, aki <akhileshrawat. ..@gmail.comwro te:
                On Aug 1, 11:17 pm, James Kanze <james.ka...@gm ail.comwrote:
                On Aug 1, 6:20 pm, aki <akhileshrawat. ..@gmail.comwro te:
                On Aug 1, 12:23 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
                [...]
                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_NE NAME_*_trans.cs v");
                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<<e ndl; // here i printed the value it is
                coming correct
                TrDebug<<"netyp e"<<netype<<end l;// here also
                TrDebug<<"nenam e"<<nename<<end l; // 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.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • aki

                  #9
                  Re: file deletion in a directory with some conditions .

                  On Aug 4, 6:15 pm, James Kanze <james.ka...@gm ail.comwrote:
                  On Aug 4, 2:07 pm, aki <akhileshrawat. ..@gmail.comwro te:
                  >
                  >
                  >
                  On Aug 1, 11:17 pm, James Kanze <james.ka...@gm ail.comwrote:
                  On Aug 1, 6:20 pm, aki <akhileshrawat. ..@gmail.comwro te:
                  On Aug 1, 12:23 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
                  [...]
                  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_NE NAME_*_trans.cs v");
                  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<<e ndl; // here i printed the value it is
                  coming correct
                  TrDebug<<"netyp e"<<netype<<end l;// here also
                  TrDebug<<"nenam e"<<nename<<end l; // 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

                  --
                  James Kanze (GABI Software) email:james.ka. ..@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  Working...