To test the pattern whether it is.jpg or .xml by comparing with the extension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RaviRajhulk
    New Member
    • Feb 2008
    • 12

    To test the pattern whether it is.jpg or .xml by comparing with the extension

    I need to write the image files in one file and (the xml and html) in another file from a common file. I am trying to compare the extensions,like if it is .jpg or .bmp it will be written to the image file and if it is .html or .xml it will be written to the app file.But I am not able to do so.My code is given below
    [code=perl]
    open (MyFileHandle," <C\:\\home\\com monfilelist.txt ");
    open (MyWebFileHandl e,">$imagefile" ) or die("File Open Error");
    open (MyAppFileHandl e,">$Appfile" ) or die("File Open Error");

    # Read till end of file.
    until((eof(MyFi leHandle)) && (seek(MyFileHan dle,0,0) != eof(MyFileHandl e)))
    {
    # Check if data exists. If empty exit with appropriate error.
    $Text = readline(MyFile Handle);
    if(defined($Tex t))
    {
    #@ArrayTemp = split("/",$Text);
    #$Ch = substr($ArrayTe mp[0],0);
    $result=rindex( $Text,".");
    $pattern=substr ($Text,$result) ;
    if($pattern eq (("jpg")||("bmp ")))
    {
    print MyWebFileHandle $Text;
    print MyWebFileHandle "\n";
    }
    elsif ($pattern eq (("xml")||("htm l")))
    {
    print MyAppFileHandle $Text;
    print MyAppFileHandle "\n";
    }

    else
    {
    print "Not an image and Html.\n";
    }
    }
    }[/CODE]
    Last edited by eWish; Mar 10 '08, 08:40 PM. Reason: Please use [CODE][/CODE] tags
  • rajiv07
    New Member
    • Jun 2007
    • 141

    #2
    [CODE=perl]open(COMMON,"c:/perl/common.txt") or die $!;

    open(IMAGE,">c:/perl/image.txt") or die $!;

    open(HTML,">c:/perl/html.txt") or die $!;

    my @commonFile=<CO MMON>;

    print IMAGE grep(/\.(jpg|bmp)$/,@commonFile);

    print HTML grep(/\.(xml|html)$/,@commonFile);[/CODE]

    Hopes it help

    Regards
    Rajiv

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      You can simplify your search script to this:
      [CODE=perl]
      open (MyFileHandle," <C\:\\home\\com monfilelist.txt ");
      open (MyWebFileHandl e,">$imagefile" ) or die("File Open Error");
      open (MyAppFileHandl e,">$Appfile" ) or die("File Open Error");
      while(<MyFileHa ndle>) {
      if((/\.jpg/)||(/\.bmp/)) {
      print MyWebFileHandle $_; }
      elsif((/\.html?/)||(/\.xml/)) {
      print MyAppFileHandle $_; }
      else {
      chomp;
      print "$_ is not an image or Html.\n"; }
      }

      [/CODE]

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Well...what Rajiv has posted is even more crisp and very well do the job.

        Comment

        • RaviRajhulk
          New Member
          • Feb 2008
          • 12

          #5
          Originally posted by rajiv07
          [CODE=perl]open(COMMON,"c:/perl/common.txt") or die $!;

          open(IMAGE,">c:/perl/image.txt") or die $!;

          open(HTML,">c:/perl/html.txt") or die $!;

          my @commonFile=<CO MMON>;

          print IMAGE grep(/\.(jpg|bmp)$/,@commonFile);

          print HTML grep(/\.(xml|html)$/,@commonFile);[/CODE]

          Hopes it help

          Regards
          Rajiv
          This is not working.help plz

          Comment

          • rajiv07
            New Member
            • Jun 2007
            • 141

            #6
            Originally posted by RaviRajhulk
            This is not working.help plz
            Its working fine for me.Have u getting any error.

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #7
              Originally posted by RaviRajhulk
              This is not working.help plz
              If so, provide a sample of contents in commonfilelist. txt .

              This may also happen if your filelist has some other data along with filename in each line or spaces inserted at the end of each line. In such cases, modify those two lines to:
              Code:
              print IMAGE grep(/\.(jpg|bmp)/,@commonFile);
               
              print HTML grep(/\.(xml|html)/,@commonFile);
              Just removed end of line($) in pattern.

              Comment

              • eWish
                Recognized Expert Contributor
                • Jul 2007
                • 973

                #8
                Originally posted by RaviRajhulk
                This is not working.help plz
                Please be more descriptive of your problem. We will need more information to be able to offer assistance. In the future posts such as this will be deleted.

                Moderator

                Comment

                • RaviRajhulk
                  New Member
                  • Feb 2008
                  • 12

                  #9
                  Originally posted by nithinpes
                  If so, provide a sample of contents in commonfilelist. txt .

                  This may also happen if your filelist has some other data along with filename in each line or spaces inserted at the end of each line. In such cases, modify those two lines to:
                  Code:
                  print IMAGE grep(/\.(jpg|bmp)/,@commonFile);
                   
                  print HTML grep(/\.(xml|html)/,@commonFile);
                  Just removed end of line($) in pattern.
                  After executing the code ,the Appfile and webfile are created but they remain empty.The data in the common file are
                  a.html
                  b.html
                  falls.html
                  html\test.html
                  templatedata\ca tegory4
                  templatedata\Li brary
                  templatedata\Ni k
                  templatedata\Pa ge
                  templatedata\po c
                  templatedata\zz _tst_sakina_mun shi_1_manifest
                  Test1.html
                  folwer.jpg
                  I need to write the html file to appfile and the image file to webfile,rest i need to neglect

                  Comment

                  • nithinpes
                    Recognized Expert Contributor
                    • Dec 2007
                    • 410

                    #10
                    Originally posted by RaviRajhulk
                    After executing the code ,the Appfile and webfile are created but they remain empty.The data in the common file are
                    a.html
                    b.html
                    falls.html
                    html\test.html
                    templatedata\ca tegory4
                    templatedata\Li brary
                    templatedata\Ni k
                    templatedata\Pa ge
                    templatedata\po c
                    templatedata\zz _tst_sakina_mun shi_1_manifest
                    Test1.html
                    folwer.jpg
                    I need to write the html file to appfile and the image file to webfile,rest i need to neglect
                    For this data, the script worked perfectly fine for me!! Don't know what is happening with you. Did you try the other method that was posted?
                    If that doesn't work, post the most recent code that you used.

                    Comment

                    • kalyanrajsista
                      New Member
                      • Mar 2008
                      • 7

                      #11
                      Please check my comments and code wrapped in code tags..

                      Comment

                      • kalyanrajsista
                        New Member
                        • Mar 2008
                        • 7

                        #12
                        I've tested the following code on unix box with the sample data provided in common.txt and found to be working fine.

                        Code:
                        #!/usr/bin/perl -w
                        
                        use strict;
                        
                        open (COMMON,"common.txt") or die $!;
                        open (IMAGE,">image.txt") or die $!;
                        open (HTML,">html.txt") or die $!;
                        my @commonFile = <COMMON>;
                        print IMAGE grep(/\.(jpg|bmp)$/, @commonFile);
                        print HTML grep(/\.(xml|html)$/, @commonFile);
                        Please note that i've removed the path of the files for my convenience as they got created from where i ran my script.

                        Kalyan

                        Comment

                        Working...