Problem in Perl program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashootoshverma
    New Member
    • Feb 2008
    • 5

    Problem in Perl program

    Hi Everyone,

    I am new in perl programming can anyone help me to give a right way to do in this program
    "Zip all .* file from a one windows directory to another directory."

    Here is my code.

    [CODE=perl]#!\usr\bin\perl
    use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
    use File::Copy;
    $Inpath = "M:\\SAPCONV\\S taging\\scripts \\";
    print ".pl files in $Inpath are:\n";
    opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
    while( defined ($file = readdir DIR) ) {
    print "glob param: $file\n" if -T "$Inpath/$file";
    }
    closedir(DIR);
    $OutPath = "G:\\sysouts\\" ;
    $tempdir = $Inpath . "scripts\\temp\ \";
    $zipfile = "scripts.zi p";
    chdir $InPath;
    @FileList = glob($file);
    my $zip = Archive::Zip->new();

    foreach (@FileList) {
    $FilePathName = $_;
    @PathnameParts = split /\\/, $FilePathName;
    $ListCount = @PathnameParts;
    $FileName = @PathnameParts[$ListCount-1];

    print "Adding File...." . $FileName . "\n";

    $zip->addFile( $FileName )
    or do {
    warn "Can't add file $FileName, $!\n";

    }
    }

    my $status = $zip->writeToFileNam ed($OutPath.$zi pfile);

    if ($status == AZ_OK) {
    print "Zipping completed successfully\n" ;

    } else {
    print "Zipping completed with errors\n";

    }[/CODE]
    Last edited by eWish; Mar 1 '08, 08:19 PM. Reason: Please use [CODE][/CODE] tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by ashootoshverma
    Hi Everyone,

    I am new in perl programming can anyone help me to give a right way to do in this program
    "Zip all .* file from a one windows directory to another directory."

    Here is my code.

    [CODE=perl]#!\usr\bin\perl
    use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
    use File::Copy;
    $Inpath = "M:\\SAPCONV\\S taging\\scripts \\";
    print ".pl files in $Inpath are:\n";
    opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
    while( defined ($file = readdir DIR) ) {
    print "glob param: $file\n" if -T "$Inpath/$file";
    }
    closedir(DIR);
    $OutPath = "G:\\sysouts\\" ;
    $tempdir = $Inpath . "scripts\\temp\ \";
    $zipfile = "scripts.zi p";
    chdir $InPath;
    @FileList = glob($file);
    my $zip = Archive::Zip->new();

    foreach (@FileList) {
    $FilePathName = $_;
    @PathnameParts = split /\\/, $FilePathName;
    $ListCount = @PathnameParts;
    $FileName = @PathnameParts[$ListCount-1];

    print "Adding File...." . $FileName . "\n";

    $zip->addFile( $FileName )
    or do {
    warn "Can't add file $FileName, $!\n";

    }
    }

    my $status = $zip->writeToFileNam ed($OutPath.$zi pfile);

    if ($status == AZ_OK) {
    print "Zipping completed successfully\n" ;

    } else {
    print "Zipping completed with errors\n";

    }[/CODE]
    So, if you run the code, are you getting any errors? Before anyone here touches this code though, you are going to have to include the pragmas "use strict" and "use warnings" and get the usual bugs out of the way first.

    REgards,

    Jeff

    Comment

    • ashootoshverma
      New Member
      • Feb 2008
      • 5

      #3
      Thanks!
      Jeff,

      I used use stirct and use warnings
      I got so many error like ,So please help me to traceout from this script.
      Thanks in advance.



      Originally posted by numberwhun
      So, if you run the code, are you getting any errors? Before anyone here touches this code though, you are going to have to include the pragmas "use strict" and "use warnings" and get the usual bugs out of the way first.

      REgards,

      Jeff

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by ashootoshverma
        Thanks!
        Jeff,

        I used use stirct and use warnings
        I got so many error like ,So please help me to traceout from this script.
        Thanks in advance.
        When you use strict pragma, you have to define all your variables as local to the script using 'my' keyword. Apart from that, I have made few changes in while loop, by including next five lines inside the loop. This worked for me.

        [CODE=perl]
        #!\usr\bin\perl
        use strict;
        use warnings;
        use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

        my ($file,$zipfile ,$zip);
        my @FileList;
        my $tempdir;

        my $Inpath = "D:\\SAP\\scrip ts\\";
        my $OutPath = "D:\\sysouts\\" ;
        print "The files in $Inpath are:\n";
        opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
        while( defined ($file = readdir DIR) ) {
        print "glob param: $file\n" if -T "$Inpath/$file";

        $tempdir = $Inpath . "scripts\\temp\ \";
        $zipfile = "scripts.zi p";
        chdir $Inpath;
        push @FileList,$file if($file=~/\.pl/); ### add only files with .pl extension
        }
        closedir(DIR);
        $zip = Archive::Zip->new();
        foreach (@FileList) {
        my $FilePathName = $_;
        my @PathnameParts = split /\\/, $FilePathName;
        my $ListCount = @PathnameParts;
        my $FileName = $PathnameParts[$ListCount-1];

        print "Adding File.... $FileName \n";

        $zip->addFile( $FileName )
        or do {
        warn "Can't add file $FileName, $!\n";

        }
        }

        my $status = $zip->writeToFileNam ed($OutPath.$zi pfile);

        if ($status == AZ_OK) {
        print "Zipping completed successfully\n" ;

        } else {
        print "Zipping completed with errors\n";

        }
        [/CODE]
        Last edited by nithinpes; Mar 4 '08, 02:11 PM. Reason: minor change in code

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Forgot to mention the reason for including the lines inside while loop. Well, you can define $tempdir and $zipfile outside the while loop and that is the best way of doing it to avoid unnecessary variable assignment with each iteration. I included these two lines by mistake.
          But the line of concern is where you define @FileList. If you define it outside, $file will be containing the last variable(last file) read and hence you can't build the filelist.

          Comment

          • ashootoshverma
            New Member
            • Feb 2008
            • 5

            #6
            First thanks for your help second it didn't extract all .pl file from different folder
            those in directory M:\\ .Actually M:\\ contain some file like .pl as well as some folders .And every folder have .pl files in it.


            Originally posted by nithinpes
            When you use strict pragma, you have to define all your variables as local to the script using 'my' keyword. Apart from that, I have made few changes in while loop, by including next five lines inside the loop. This worked for me.

            [CODE=perl]
            #!\usr\bin\perl
            use strict;
            use warnings;
            use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

            my ($file,$zipfile ,$zip);
            my @FileList;
            my $tempdir;

            my $Inpath = "D:\\SAP\\scrip ts\\";
            my $OutPath = "D:\\sysouts\\" ;
            print "The files in $Inpath are:\n";
            opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
            while( defined ($file = readdir DIR) ) {
            print "glob param: $file\n" if -T "$Inpath/$file";

            $tempdir = $Inpath . "scripts\\temp\ \";
            $zipfile = "scripts.zi p";
            chdir $Inpath;
            push @FileList,$file if($file=~/\.pl/); ### add only files with .pl extension
            }
            closedir(DIR);
            $zip = Archive::Zip->new();
            foreach (@FileList) {
            my $FilePathName = $_;
            my @PathnameParts = split /\\/, $FilePathName;
            my $ListCount = @PathnameParts;
            my $FileName = $PathnameParts[$ListCount-1];

            print "Adding File.... $FileName \n";

            $zip->addFile( $FileName )
            or do {
            warn "Can't add file $FileName, $!\n";

            }
            }

            my $status = $zip->writeToFileNam ed($OutPath.$zi pfile);

            if ($status == AZ_OK) {
            print "Zipping completed successfully\n" ;

            } else {
            print "Zipping completed with errors\n";

            }
            [/CODE]

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #7
              Originally posted by ashootoshverma
              First thanks for your help second it didn't extract all .pl file from different folder
              those in directory M:\\ .Actually M:\\ contain some file like .pl as well as some folders .And every folder have .pl files in it.
              Well, you had not mentioned that in your previous description. In that case, you can check for directories inside the main folder and further read from these sub-directories.

              [CODE=perl]
              #!\usr\bin\perl
              use strict;
              use warnings;
              use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

              my ($zipfile,$zip) ;
              my @FileList;
              my $tempdir;
              my @subdir;

              my $Inpath = "D:\\SAP\\scrip ts\\";
              my $OutPath = "D:\\sysouts\\" ;
              print ".pl files in $Inpath are:\n";
              opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
              while( defined (my $file = readdir DIR) ) {
              print "glob param: $file\n" if -T "$Inpath/$file";

              push @subdir,$file if(-d "$Inpath\\$file "); ##taking out sub-folders

              push @FileList,$file if($file=~/\.pl/);


              }
              closedir(DIR);

              foreach(@subdir ) ##read from sub-folders
              {
              next if($_ =~/^\.*$/); ## remove . and ..
              opendir(SUB, "$Inpath/$_") or die "Can't open $Inpath: $!";
              while( defined (my $subfile = readdir SUB) ) {
              push @FileList,$subf ile if($subfile=~/\.pl/);
              }
              closedir(SUB);
              }

              $tempdir = $Inpath . "scripts\\temp\ \";
              $zipfile = "scripts.zi p";
              $zip = Archive::Zip->new();

              foreach (@FileList) {
              my $FilePathName = $_;
              my @PathnameParts = split /\\/, $FilePathName;
              my $ListCount = @PathnameParts;
              my $FileName = $PathnameParts[$ListCount-1];

              print "Adding File.... $FileName \n";

              $zip->addFile( $FileName )
              or do {
              warn "Can't add file $FileName, $!\n";

              }
              }

              my $status = $zip->writeToFileNam ed($OutPath.$zi pfile);

              if ($status == AZ_OK) {
              print "Zipping completed successfully\n" ;

              } else {
              print "Zipping completed with errors\n";

              }

              [/CODE]

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                Also, the above code will work for one level of hierarchy i.e. folder having perl files and sub-folders having perl files. If the sub-folders have further sub-folders and you want to traverse entire directory tree, make use of File::Find module.

                Comment

                • ashootoshverma
                  New Member
                  • Feb 2008
                  • 5

                  #9
                  Hi Thanks again,
                  But the error message what I got is
                  Adding file .........xyz.pl
                  Cann't add file xyz.pl~ No such file or directory

                  Comment

                  • ashootoshverma
                    New Member
                    • Feb 2008
                    • 5

                    #10
                    Hi All,

                    Can anyone help me to findout solution of my problem because its almost a long time for my problem put in forum.

                    Originally posted by ashootoshverma
                    Hi Thanks again,
                    But the error message what I got is
                    Adding file .........xyz.pl
                    Cann't add file xyz.pl~ No such file or directory

                    Comment

                    • nithinpes
                      Recognized Expert Contributor
                      • Dec 2007
                      • 410

                      #11
                      Originally posted by ashootoshverma
                      Hi All,

                      Can anyone help me to findout solution of my problem because its almost a long time for my problem put in forum.

                      Try this:

                      [CODE=perl]
                      #!\usr\bin\perl
                      use strict;
                      use warnings;
                      use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

                      my ($zipfile,$zip) ;
                      my @FileList;
                      my $tempdir;
                      my @subdir;

                      my $Inpath = "D:\\SAP\\scrip ts\\";
                      my $OutPath = "D:\\sysouts\\" ;
                      print ".pl files in $Inpath are:\n";
                      opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
                      while( defined (my $file = readdir DIR) ) {
                      print "glob param: $file\n" if -T "$Inpath/$file";

                      push @subdir,$file if(-d $Inpath.$file); ##taking out sub-folders

                      push @FileList,$Inpa th.$file if($file=~/\.pl/);


                      }
                      closedir(DIR);

                      foreach(@subdir ) ##read from sub-folders
                      {
                      next if($_ =~/^\.*$/);
                      opendir(SUB, "$Inpath/$_") or die "Can't open $Inpath: $!";
                      while( defined (my $subfile = readdir SUB) ) {
                      push @FileList,$Inpa th."$_\\$subfil e" if($subfile=~/\.pl/); }
                      closedir(SUB);
                      }

                      $tempdir = $Inpath . "scripts\\temp\ \";
                      $zipfile = "scripts.zi p";
                      $zip = Archive::Zip->new();

                      foreach (@FileList) {
                      my $FileName =$_;
                      print "Adding File.... $FileName \n";

                      $zip->addFile( $FileName )
                      or do {
                      warn "Can't add file $FileName, $!\n";

                      }
                      }

                      my $status = $zip->writeToFileNam ed($OutPath.$zi pfile);

                      if ($status == AZ_OK) {
                      print "Zipping completed successfully\n" ;

                      } else {
                      print "Zipping completed with errors\n";

                      }
                      [/CODE]
                      Last edited by nithinpes; Mar 18 '08, 12:22 PM. Reason: edited code

                      Comment

                      Working...