Loading ...
Sorry, an error occurred while loading the content.

Script to print all the files with .zip extentsion in a given directory

Expand Messages
  • GoPi
    Hi, I am trying to write a perl script to print all the given files in a directory with .zip extension which contains many subdirectories,so basically I have
    Message 1 of 10 , May 29 7:55 PM
    • 0 Attachment
      Hi,

      I am trying to write a perl script to print all the given files in a directory with .zip extension which contains many subdirectories,so basically I have to do a recursive search in the given directory and find all the files with .zip extension and print all of them.Can someone help me to get started?
    • Kenneth Wolcott
      ... Hi; What perl code have you written already? Ken [Non-text portions of this message have been removed]
      Message 2 of 10 , May 29 8:09 PM
      • 0 Attachment
        On Sat, May 29, 2010 at 19:55, GoPi <gopichand84@...> wrote:

        > I am trying to write a perl script to print all the given files in a
        > directory with .zip extension which contains many subdirectories,so
        > basically I have to do a recursive search in the given directory and find
        > all the files with .zip extension and print all of them.Can someone help me
        > to get started?
        >

        Hi;

        What perl code have you written already?

        Ken


        [Non-text portions of this message have been removed]
      • gopi chand
        I just got this over the net ,tried to run with  perl grep.pl (?i)sorting .zip but didn t work.Can you pls help? use strict; use warnings; use Cwd; use
        Message 3 of 10 , May 29 8:25 PM
        • 0 Attachment
          I just got this over the net ,tried to run with  perl grep.pl (?i)sorting .zip but didn't work.Can you pls help?



          use strict;
          use warnings;
          use Cwd;

          use File::Find;

          my $search_pattern=$ARGV[0];
          my $file_pattern =$ARGV[1];

          find(\&d, cwd);

          sub d {

          my $file = $File::Find::name;

          $file =~ s,/,\\,g;

          return unless -f $file;
          return unless $file =~ /$file_pattern/;

          open F, $file or print "couldn't open $file\n" && return;

          while (<F>) {
          if (my ($found) = m/($search_pattern)/o) {
          print "found $found in $file\n";
          last;
          }
          }

          close F;
          }





          --- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...> wrote:

          From: Kenneth Wolcott <kennethwolcott@...>
          Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
          To: perl-beginner@yahoogroups.com
          Date: Saturday, May 29, 2010, 8:09 PM







           









          On Sat, May 29, 2010 at 19:55, GoPi <gopichand84@...> wrote:



          > I am trying to write a perl script to print all the given files in a

          > directory with .zip extension which contains many subdirectories,so

          > basically I have to do a recursive search in the given directory and find

          > all the files with .zip extension and print all of them.Can someone help me

          > to get started?

          >



          Hi;



          What perl code have you written already?



          Ken



          [Non-text portions of this message have been removed]

























          [Non-text portions of this message have been removed]
        • Kenneth Wolcott
          ... Did you look at the code that you grabbed off the net and make any conclusions about how it works? Do you know how to use this code? Where you grabbed
          Message 4 of 10 , May 29 8:54 PM
          • 0 Attachment
            On Sat, May 29, 2010 at 20:25, gopi chand <gopichand84@...> wrote:

            >
            >
            > I just got this over the net ,tried to run with perl grep.pl (?i)sorting
            > .zip but didn't work.Can you pls help?
            >
            > use strict;
            > use warnings;
            > use Cwd;
            >
            > use File::Find;
            >
            > my $search_pattern=$ARGV[0];
            > my $file_pattern =$ARGV[1];
            >
            > find(\&d, cwd);
            >
            > sub d {
            >
            > my $file = $File::Find::name;
            >
            > $file =~ s,/,\\,g;
            >
            > return unless -f $file;
            > return unless $file =~ /$file_pattern/;
            >
            > open F, $file or print "couldn't open $file\n" && return;
            >
            > while (<F>) {
            > if (my ($found) = m/($search_pattern)/o) {
            > print "found $found in $file\n";
            > last;
            > }
            > }
            >
            > close F;
            > }
            >

            Did you look at the code that you grabbed off the 'net and make any
            conclusions about how it works?

            Do you know how to use this code?

            Where you grabbed this code from, was there any documentation on how to
            use it?

            It would help if the indentation was still in the code :-(

            Let's make your problem easier so that you could solve a sub-problem, then
            later use the knowledge gained to solve the larger problem.

            How would you print all of the filenames with a .zip suffix that were in
            the same directory using perl?

            I don't want to solve this problem *for* you; I want to *help* you solve
            it *yourself* :-)

            Ken


            [Non-text portions of this message have been removed]
          • gopi chand
            Hi Kenneth, Answered inline with [me] ... From: Kenneth Wolcott Subject: Re: [PBML] Script to print all the files with .zip
            Message 5 of 10 , May 29 9:13 PM
            • 0 Attachment
              Hi Kenneth,

              Answered inline with [me]



              --- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...> wrote:

              From: Kenneth Wolcott <kennethwolcott@...>
              Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
              To: perl-beginner@yahoogroups.com
              Date: Saturday, May 29, 2010, 8:54 PM







               









              On Sat, May 29, 2010 at 20:25, gopi chand <gopichand84@...> wrote:



              >

              >

              > I just got this over the net ,tried to run with perl grep.pl (?i)sorting

              > .zip but didn't work.Can you pls help?

              >

              > use strict;

              > use warnings;

              > use Cwd;

              >

              > use File::Find;

              >

              > my $search_pattern=$ARGV[0];

              > my $file_pattern =$ARGV[1];

              >

              > find(\&d, cwd);

              >

              > sub d {

              >

              > my $file = $File::Find::name;

              >

              > $file =~ s,/,\\,g;

              >

              > return unless -f $file;

              > return unless $file =~ /$file_pattern/;

              >

              > open F, $file or print "couldn't open $file\n" && return;

              >

              > while (<F>) {

              > if (my ($found) = m/($search_pattern)/o) {

              > print "found $found in $file\n";

              > last;

              > }

              > }

              >

              > close F;

              > }

              >



              Did you look at the code that you grabbed off the 'net and make any

              conclusions about how it works?[me]yup.I understand how it works



              Do you know how to use this code?[me]I don't

              Where you grabbed this code from, was there any documentation on how to

              use it?

              [me]No.

              It would help if the indentation was still in the code :-(



              Let's make your problem easier so that you could solve a sub-problem, then

              later use the knowledge gained to solve the larger problem.



              How would you print all of the filenames with a .zip suffix that were in

              the same directory using perl?[me]The below code prints all the files with a .zip extension but it's not doing a recursive search into subdirectories
              #!/usr/bin/perl -w

              @files = <*.zip>;
              foreach $file (@files) {
                print $file . "\n";
              }


              I don't want to solve this problem *for* you; I want to *help* you solve

              it *yourself* :-)



              Ken



              [Non-text portions of this message have been removed]

























              [Non-text portions of this message have been removed]
            • Kenneth Wolcott
              ... Ok, I think you solved the easier sub-problem just fine. What is the output that you obtain when you run the script that you found on the net? I don t
              Message 6 of 10 , May 29 9:34 PM
              • 0 Attachment
                On Sat, May 29, 2010 at 21:13, gopi chand <gopichand84@...> wrote:

                >
                >
                >
                > Hi Kenneth,
                >
                > Answered inline with [me]
                >
                >
                > --- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...<kennethwolcott%40gmail.com>>
                > wrote:
                >
                > From: Kenneth Wolcott <kennethwolcott@...<kennethwolcott%40gmail.com>
                > >
                > Subject: Re: [PBML] Script to print all the files with .zip extentsion in a
                > given directory
                > To: perl-beginner@yahoogroups.com <perl-beginner%40yahoogroups.com>
                > Date: Saturday, May 29, 2010, 8:54 PM
                >
                >
                >
                >
                > On Sat, May 29, 2010 at 20:25, gopi chand <gopichand84@...<gopichand84%40yahoo.com>>
                > wrote:
                >
                > >
                >
                > >
                >
                > > I just got this over the net ,tried to run with perl grep.pl (?i)sorting
                >
                > > .zip but didn't work.Can you pls help?
                >
                > >
                >
                > > use strict;
                >
                > > use warnings;
                >
                > > use Cwd;
                >
                > >
                >
                > > use File::Find;
                >
                > >
                >
                > > my $search_pattern=$ARGV[0];
                >
                > > my $file_pattern =$ARGV[1];
                >
                > >
                >
                > > find(\&d, cwd);
                >
                > >
                >
                > > sub d {
                >
                > >
                >
                > > my $file = $File::Find::name;
                >
                > >
                >
                > > $file =~ s,/,\\,g;
                >
                > >
                >
                > > return unless -f $file;
                >
                > > return unless $file =~ /$file_pattern/;
                >
                > >
                >
                > > open F, $file or print "couldn't open $file\n" && return;
                >
                > >
                >
                > > while (<F>) {
                >
                > > if (my ($found) = m/($search_pattern)/o) {
                >
                > > print "found $found in $file\n";
                >
                > > last;
                >
                > > }
                >
                > > }
                >
                > >
                >
                > > close F;
                >
                > > }
                >
                > >
                >
                > Did you look at the code that you grabbed off the 'net and make any
                >
                > conclusions about how it works?[me]yup.I understand how it works
                >
                > Do you know how to use this code?[me]I don't
                >
                >
                > Where you grabbed this code from, was there any documentation on how to
                >
                > use it?
                >
                > [me]No.
                >
                >
                > It would help if the indentation was still in the code :-(
                >
                > Let's make your problem easier so that you could solve a sub-problem, then
                >
                > later use the knowledge gained to solve the larger problem.
                >
                > How would you print all of the filenames with a .zip suffix that were in
                >
                > the same directory using perl?[me]The below code prints all the files with
                > a .zip extension but it's not doing a recursive search into subdirectories
                > #!/usr/bin/perl -w
                >
                > @files = <*.zip>;
                > foreach $file (@files) {
                > print $file . "\n";
                >
                > }
                >
                > I don't want to solve this problem *for* you; I want to *help* you solve
                >
                > it *yourself* :-)
                >
                > Ken
                >

                Ok, I think you solved the easier sub-problem just fine.

                What is the output that you obtain when you run the script that you found
                on the 'net?

                I don't precisely understand how you invoked the script, perhaps there
                were some unicode characters that displayed incorrectly in my gmail screen
                that confused me, so show me the output that you say "didn't work".

                Please be precise what you mean about "didn't work".

                Ken


                [Non-text portions of this message have been removed]
              • gopi chand
                Hi Kenneth This script looks a little bit better,when I run the below script the output is below.Can you help getting rid of this erro? OUTPUT:main::recurse()
                Message 7 of 10 , May 29 10:23 PM
                • 0 Attachment
                  Hi Kenneth

                  This script looks a little bit better,when I run the below script the output is below.Can you help getting rid of this erro?

                  OUTPUT:main::recurse() called too early to check prototype at greep.pl line 20.
                  Use of uninitialized value in pattern match (m//) at greep.pl line 9./

                  #!/usr/bin/perl -w

                  use strict;

                  sub recurse($) {
                    my($path) = @_;

                    ## append a trailing / if it's not there
                    $path .= '/' if($path !~ /\/$/);

                    ## print the directory being searched
                    print $path,"\n";

                    ## loop through the files contained in the directory
                    for my $eachFile (glob($path.'*.zip')) {

                      ## if the file is a directory
                      if( -d $eachFile) {
                        ## pass the directory to the routine ( recursion )
                        recurse($eachFile);
                      } else {

                        ## print the file ... tabbed for readability
                        print "\t",$eachFile,"\n";
                      }
                    }
                  }

                  ## initial call ... $ARGV[0] is the first command line argument
                  recurse($ARGV[0]);




                  --- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...> wrote:

                  From: Kenneth Wolcott <kennethwolcott@...>
                  Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
                  To: perl-beginner@yahoogroups.com
                  Date: Saturday, May 29, 2010, 9:34 PM







                   









                  On Sat, May 29, 2010 at 21:13, gopi chand <gopichand84@...> wrote:



                  >

                  >

                  >

                  > Hi Kenneth,

                  >

                  > Answered inline with [me]

                  >

                  >

                  > --- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...<kennethwolcott%40gmail.com>>

                  > wrote:

                  >

                  > From: Kenneth Wolcott <kennethwolcott@...<kennethwolcott%40gmail.com>

                  > >

                  > Subject: Re: [PBML] Script to print all the files with .zip extentsion in a

                  > given directory

                  > To: perl-beginner@yahoogroups.com <perl-beginner%40yahoogroups.com>

                  > Date: Saturday, May 29, 2010, 8:54 PM

                  >

                  >

                  >

                  >

                  > On Sat, May 29, 2010 at 20:25, gopi chand <gopichand84@...<gopichand84%40yahoo.com>>

                  > wrote:

                  >

                  > >

                  >

                  > >

                  >

                  > > I just got this over the net ,tried to run with perl grep.pl (?i)sorting

                  >

                  > > .zip but didn't work.Can you pls help?

                  >

                  > >

                  >

                  > > use strict;

                  >

                  > > use warnings;

                  >

                  > > use Cwd;

                  >

                  > >

                  >

                  > > use File::Find;

                  >

                  > >

                  >

                  > > my $search_pattern=$ARGV[0];

                  >

                  > > my $file_pattern =$ARGV[1];

                  >

                  > >

                  >

                  > > find(\&d, cwd);

                  >

                  > >

                  >

                  > > sub d {

                  >

                  > >

                  >

                  > > my $file = $File::Find::name;

                  >

                  > >

                  >

                  > > $file =~ s,/,\\,g;

                  >

                  > >

                  >

                  > > return unless -f $file;

                  >

                  > > return unless $file =~ /$file_pattern/;

                  >

                  > >

                  >

                  > > open F, $file or print "couldn't open $file\n" && return;

                  >

                  > >

                  >

                  > > while (<F>) {

                  >

                  > > if (my ($found) = m/($search_pattern)/o) {

                  >

                  > > print "found $found in $file\n";

                  >

                  > > last;

                  >

                  > > }

                  >

                  > > }

                  >

                  > >

                  >

                  > > close F;

                  >

                  > > }

                  >

                  > >

                  >

                  > Did you look at the code that you grabbed off the 'net and make any

                  >

                  > conclusions about how it works?[me]yup.I understand how it works

                  >

                  > Do you know how to use this code?[me]I don't

                  >

                  >

                  > Where you grabbed this code from, was there any documentation on how to

                  >

                  > use it?

                  >

                  > [me]No.

                  >

                  >

                  > It would help if the indentation was still in the code :-(

                  >

                  > Let's make your problem easier so that you could solve a sub-problem, then

                  >

                  > later use the knowledge gained to solve the larger problem.

                  >

                  > How would you print all of the filenames with a .zip suffix that were in

                  >

                  > the same directory using perl?[me]The below code prints all the files with

                  > a .zip extension but it's not doing a recursive search into subdirectories

                  > #!/usr/bin/perl -w

                  >

                  > @files = <*.zip>;

                  > foreach $file (@files) {

                  > print $file . "\n";

                  >

                  > }

                  >

                  > I don't want to solve this problem *for* you; I want to *help* you solve

                  >

                  > it *yourself* :-)

                  >

                  > Ken

                  >



                  Ok, I think you solved the easier sub-problem just fine.



                  What is the output that you obtain when you run the script that you found

                  on the 'net?



                  I don't precisely understand how you invoked the script, perhaps there

                  were some unicode characters that displayed incorrectly in my gmail screen

                  that confused me, so show me the output that you say "didn't work".



                  Please be precise what you mean about "didn't work".



                  Ken



                  [Non-text portions of this message have been removed]

























                  [Non-text portions of this message have been removed]
                • Kenneth Wolcott
                  ... I see that you have chosen a different script :-) Here s my advice: There s a very old provided tool with perl called find2perl . My understanding of the
                  Message 8 of 10 , May 31 5:53 PM
                  • 0 Attachment
                    On Sat, May 29, 2010 at 22:23, gopi chand <gopichand84@...> wrote:

                    >
                    >
                    > Hi Kenneth
                    >
                    > This script looks a little bit better,when I run the below script the
                    > output is below.Can you help getting rid of this erro?
                    >
                    > OUTPUT:main::recurse() called too early to check prototype at greep.plline 20.
                    > Use of uninitialized value in pattern match (m//) at greep.pl line 9./
                    >
                    > #!/usr/bin/perl -w
                    >
                    > use strict;
                    >
                    > sub recurse($) {
                    > my($path) = @_;
                    >
                    > ## append a trailing / if it's not there
                    > $path .= '/' if($path !~ /\/$/);
                    >
                    > ## print the directory being searched
                    > print $path,"\n";
                    >
                    > ## loop through the files contained in the directory
                    > for my $eachFile (glob($path.'*.zip')) {
                    >
                    > ## if the file is a directory
                    > if( -d $eachFile) {
                    > ## pass the directory to the routine ( recursion )
                    > recurse($eachFile);
                    > } else {
                    >
                    > ## print the file ... tabbed for readability
                    > print "\t",$eachFile,"\n";
                    > }
                    > }
                    > }
                    >
                    > ## initial call ... $ARGV[0] is the first command line argument
                    > recurse($ARGV[0]);
                    >

                    I see that you have chosen a different script :-)

                    Here's my advice:

                    There's a very old provided tool with perl called "find2perl".

                    My understanding of the original problem you wish to solve is solved by
                    "find <dir> -name \*.zip".

                    So, using "find2perl" with aforementioned find command results in this:

                    find2perl . -name \*.zip
                    #! /usr/bin/perl -w
                    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
                    if 0; #$running_under_some_shell

                    use strict;
                    use File::Find ();

                    # Set the variable $File::Find::dont_use_nlink if you're using AFS,
                    # since AFS cheats.

                    # for the convenience of &wanted calls, including -eval statements:
                    use vars qw/*name *dir *prune/;
                    *name = *File::Find::name;
                    *dir = *File::Find::dir;
                    *prune = *File::Find::prune;

                    sub wanted;

                    # Traverse desired filesystems
                    File::Find::find({wanted => \&wanted}, '.');
                    exit;

                    sub wanted {
                    /^.*\.zip\z/s
                    && print("$name\n");
                    }

                    What do you think of this?

                    Ken


                    [Non-text portions of this message have been removed]
                  • gopi chand
                    Thanks a lot Kenneth.It worked.BTW where to get the find2perl tool ... From: Kenneth Wolcott Subject: Re: [PBML] Script to print all
                    Message 9 of 10 , Jun 1, 2010
                    • 0 Attachment
                      Thanks a lot Kenneth.It worked.BTW where to get the find2perl tool



                      --- On Mon, 5/31/10, Kenneth Wolcott <kennethwolcott@...> wrote:

                      From: Kenneth Wolcott <kennethwolcott@...>
                      Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
                      To: perl-beginner@yahoogroups.com
                      Date: Monday, May 31, 2010, 5:53 PM







                       









                      On Sat, May 29, 2010 at 22:23, gopi chand <gopichand84@...> wrote:



                      >

                      >

                      > Hi Kenneth

                      >

                      > This script looks a little bit better,when I run the below script the

                      > output is below.Can you help getting rid of this erro?

                      >

                      > OUTPUT:main::recurse() called too early to check prototype at greep.plline 20.

                      > Use of uninitialized value in pattern match (m//) at greep.pl line 9./

                      >

                      > #!/usr/bin/perl -w

                      >

                      > use strict;

                      >

                      > sub recurse($) {

                      > my($path) = @_;

                      >

                      > ## append a trailing / if it's not there

                      > $path .= '/' if($path !~ /\/$/);

                      >

                      > ## print the directory being searched

                      > print $path,"\n";

                      >

                      > ## loop through the files contained in the directory

                      > for my $eachFile (glob($path.'*.zip')) {

                      >

                      > ## if the file is a directory

                      > if( -d $eachFile) {

                      > ## pass the directory to the routine ( recursion )

                      > recurse($eachFile);

                      > } else {

                      >

                      > ## print the file ... tabbed for readability

                      > print "\t",$eachFile,"\n";

                      > }

                      > }

                      > }

                      >

                      > ## initial call ... $ARGV[0] is the first command line argument

                      > recurse($ARGV[0]);

                      >



                      I see that you have chosen a different script :-)



                      Here's my advice:



                      There's a very old provided tool with perl called "find2perl".



                      My understanding of the original problem you wish to solve is solved by

                      "find <dir> -name \*.zip".



                      So, using "find2perl" with aforementioned find command results in this:



                      find2perl . -name \*.zip

                      #! /usr/bin/perl -w

                      eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'

                      if 0; #$running_under_some_shell



                      use strict;

                      use File::Find ();



                      # Set the variable $File::Find::dont_use_nlink if you're using AFS,

                      # since AFS cheats.



                      # for the convenience of &wanted calls, including -eval statements:

                      use vars qw/*name *dir *prune/;

                      *name = *File::Find::name;

                      *dir = *File::Find::dir;

                      *prune = *File::Find::prune;



                      sub wanted;



                      # Traverse desired filesystems

                      File::Find::find({wanted => \&wanted}, '.');

                      exit;



                      sub wanted {

                      /^.*\.zip\z/s

                      && print("$name\n");

                      }



                      What do you think of this?



                      Ken



                      [Non-text portions of this message have been removed]

























                      [Non-text portions of this message have been removed]
                    • merlyn@stonehenge.com
                      ... gopi Thanks a lot Kenneth.It worked.BTW where to get the find2perl tool Same directory as Perl is in. Probably /usr/bin or /usr/local/bin. If Perl is in
                      Message 10 of 10 , Jun 1, 2010
                      • 0 Attachment
                        >>>>> "gopi" == gopi chand <gopichand84@...> writes:

                        gopi> Thanks a lot Kenneth.It worked.BTW where to get the find2perl tool

                        Same directory as Perl is in. Probably /usr/bin or /usr/local/bin.

                        If Perl is in your path, so should find2perl be.

                        --
                        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
                      Your message has been successfully submitted and would be delivered to recipients shortly.