By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

Problem in Perl program

P: 5
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.

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
  3. use File::Copy;
  4. $Inpath = "M:\\SAPCONV\\Staging\\scripts\\";
  5. print ".pl files in $Inpath are:\n";
  6. opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
  7. while( defined ($file = readdir DIR) ) {
  8.     print "glob param: $file\n" if -T "$Inpath/$file";
  9. }
  10. closedir(DIR);
  11. $OutPath  = "G:\\sysouts\\";
  12. $tempdir = $Inpath . "scripts\\temp\\";
  13. $zipfile = "scripts.zip";
  14. chdir $InPath;
  15. @FileList = glob($file);
  16. my $zip = Archive::Zip->new();
  17.  
  18. foreach (@FileList) {
  19.     $FilePathName = $_;
  20.     @PathnameParts = split /\\/, $FilePathName;
  21.     $ListCount = @PathnameParts;
  22.     $FileName = @PathnameParts[$ListCount-1];
  23.  
  24.     print "Adding File...." . $FileName . "\n";
  25.  
  26.     $zip->addFile( $FileName )
  27.         or do {
  28.         warn "Can't add file $FileName, $!\n";
  29.  
  30.     }
  31. }
  32.  
  33. my $status = $zip->writeToFileNamed($OutPath.$zipfile);
  34.  
  35. if ($status == AZ_OK) {
  36.     print "Zipping completed successfully\n";
  37.  
  38. } else {
  39.     print "Zipping completed with errors\n";
  40.  
  41. }
Mar 1 '08 #1
Share this Question
Share on Google+
10 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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.

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
  3. use File::Copy;
  4. $Inpath = "M:\\SAPCONV\\Staging\\scripts\\";
  5. print ".pl files in $Inpath are:\n";
  6. opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
  7. while( defined ($file = readdir DIR) ) {
  8.     print "glob param: $file\n" if -T "$Inpath/$file";
  9. }
  10. closedir(DIR);
  11. $OutPath  = "G:\\sysouts\\";
  12. $tempdir = $Inpath . "scripts\\temp\\";
  13. $zipfile = "scripts.zip";
  14. chdir $InPath;
  15. @FileList = glob($file);
  16. my $zip = Archive::Zip->new();
  17.  
  18. foreach (@FileList) {
  19.     $FilePathName = $_;
  20.     @PathnameParts = split /\\/, $FilePathName;
  21.     $ListCount = @PathnameParts;
  22.     $FileName = @PathnameParts[$ListCount-1];
  23.  
  24.     print "Adding File...." . $FileName . "\n";
  25.  
  26.     $zip->addFile( $FileName )
  27.         or do {
  28.         warn "Can't add file $FileName, $!\n";
  29.  
  30.     }
  31. }
  32.  
  33. my $status = $zip->writeToFileNamed($OutPath.$zipfile);
  34.  
  35. if ($status == AZ_OK) {
  36.     print "Zipping completed successfully\n";
  37.  
  38. } else {
  39.     print "Zipping completed with errors\n";
  40.  
  41. }
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
Mar 1 '08 #2

P: 5
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.



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
Mar 4 '08 #3

nithinpes
Expert 100+
P: 410
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.

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use strict;
  3. use warnings;
  4. use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
  5.  
  6. my ($file,$zipfile,$zip);
  7. my @FileList;  
  8. my $tempdir;
  9.  
  10. my $Inpath = "D:\\SAP\\scripts\\";
  11. my $OutPath  = "D:\\sysouts\\";
  12. print "The files in $Inpath are:\n"; 
  13. opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
  14. while( defined ($file = readdir DIR) ) {
  15.     print "glob param: $file\n" if -T "$Inpath/$file";
  16.  
  17. $tempdir = $Inpath . "scripts\\temp\\";
  18. $zipfile = "scripts.zip";
  19. chdir $Inpath;
  20. push @FileList,$file if($file=~/\.pl/); ### add only files with .pl extension
  21. }
  22. closedir(DIR); 
  23. $zip = Archive::Zip->new();
  24. foreach (@FileList) {
  25.     my $FilePathName = $_;
  26.     my @PathnameParts = split /\\/, $FilePathName;
  27.     my $ListCount = @PathnameParts;
  28.     my $FileName = $PathnameParts[$ListCount-1];
  29.  
  30.     print "Adding File.... $FileName \n";
  31.  
  32.     $zip->addFile( $FileName )
  33.         or do {
  34.         warn "Can't add file $FileName, $!\n";
  35.  
  36.     }
  37. }
  38.  
  39. my $status = $zip->writeToFileNamed($OutPath.$zipfile);
  40.  
  41. if ($status == AZ_OK) {
  42.     print "Zipping completed successfully\n";
  43.  
  44. } else {
  45.     print "Zipping completed with errors\n";
  46.  
  47. }
  48.  
Mar 4 '08 #4

nithinpes
Expert 100+
P: 410
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.
Mar 4 '08 #5

P: 5
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.


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.

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use strict;
  3. use warnings;
  4. use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
  5.  
  6. my ($file,$zipfile,$zip);
  7. my @FileList;  
  8. my $tempdir;
  9.  
  10. my $Inpath = "D:\\SAP\\scripts\\";
  11. my $OutPath  = "D:\\sysouts\\";
  12. print "The files in $Inpath are:\n"; 
  13. opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
  14. while( defined ($file = readdir DIR) ) {
  15.     print "glob param: $file\n" if -T "$Inpath/$file";
  16.  
  17. $tempdir = $Inpath . "scripts\\temp\\";
  18. $zipfile = "scripts.zip";
  19. chdir $Inpath;
  20. push @FileList,$file if($file=~/\.pl/); ### add only files with .pl extension
  21. }
  22. closedir(DIR); 
  23. $zip = Archive::Zip->new();
  24. foreach (@FileList) {
  25.     my $FilePathName = $_;
  26.     my @PathnameParts = split /\\/, $FilePathName;
  27.     my $ListCount = @PathnameParts;
  28.     my $FileName = $PathnameParts[$ListCount-1];
  29.  
  30.     print "Adding File.... $FileName \n";
  31.  
  32.     $zip->addFile( $FileName )
  33.         or do {
  34.         warn "Can't add file $FileName, $!\n";
  35.  
  36.     }
  37. }
  38.  
  39. my $status = $zip->writeToFileNamed($OutPath.$zipfile);
  40.  
  41. if ($status == AZ_OK) {
  42.     print "Zipping completed successfully\n";
  43.  
  44. } else {
  45.     print "Zipping completed with errors\n";
  46.  
  47. }
  48.  
Mar 5 '08 #6

nithinpes
Expert 100+
P: 410
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.

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use strict;
  3. use warnings;
  4. use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
  5.  
  6. my ($zipfile,$zip);
  7. my @FileList;  
  8. my $tempdir;
  9. my @subdir;
  10.  
  11. my $Inpath = "D:\\SAP\\scripts\\";
  12. my $OutPath  = "D:\\sysouts\\";
  13. print ".pl files in $Inpath are:\n";
  14. opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
  15. while( defined (my $file = readdir DIR) ) {
  16.     print "glob param: $file\n" if -T "$Inpath/$file";
  17.  
  18. push @subdir,$file if(-d "$Inpath\\$file"); ##taking out sub-folders
  19.  
  20. push @FileList,$file if($file=~/\.pl/);
  21.  
  22.  
  23. }
  24. closedir(DIR);
  25.  
  26. foreach(@subdir) ##read from sub-folders
  27. {
  28.    next if($_ =~/^\.*$/);  ## remove . and ..
  29.    opendir(SUB, "$Inpath/$_") or die "Can't open $Inpath: $!";
  30.    while( defined (my $subfile = readdir SUB) ) {
  31.     push @FileList,$subfile if($subfile=~/\.pl/); 
  32.        }
  33.     closedir(SUB);
  34. }
  35.  
  36. $tempdir = $Inpath . "scripts\\temp\\";
  37. $zipfile = "scripts.zip";
  38. $zip = Archive::Zip->new();
  39.  
  40. foreach (@FileList) {
  41.     my $FilePathName = $_;
  42.     my @PathnameParts = split /\\/, $FilePathName;
  43.     my $ListCount = @PathnameParts;
  44.     my $FileName = $PathnameParts[$ListCount-1];
  45.  
  46.     print "Adding File.... $FileName \n";
  47.  
  48.     $zip->addFile( $FileName )
  49.         or do {
  50.         warn "Can't add file $FileName, $!\n";
  51.  
  52.     }
  53. }
  54.  
  55. my $status = $zip->writeToFileNamed($OutPath.$zipfile);
  56.  
  57. if ($status == AZ_OK) {
  58.     print "Zipping completed successfully\n";
  59.  
  60. } else {
  61.     print "Zipping completed with errors\n";
  62.  
  63. }
  64.  
  65.  
Mar 5 '08 #7

nithinpes
Expert 100+
P: 410
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.
Mar 5 '08 #8

P: 5
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
Mar 6 '08 #9

P: 5
Hi All,

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

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
Mar 18 '08 #10

nithinpes
Expert 100+
P: 410
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:

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use strict;
  3. use warnings;
  4. use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
  5.  
  6. my ($zipfile,$zip);
  7. my @FileList;  
  8. my $tempdir;
  9. my @subdir;
  10.  
  11. my $Inpath = "D:\\SAP\\scripts\\";
  12. my $OutPath  = "D:\\sysouts\\";
  13. print ".pl files in $Inpath are:\n";
  14. opendir(DIR, $Inpath) or die "Can't open $Inpath: $!";
  15. while( defined (my $file = readdir DIR) ) {
  16.     print "glob param: $file\n" if -T "$Inpath/$file";
  17.  
  18. push @subdir,$file if(-d $Inpath.$file); ##taking out sub-folders
  19.  
  20. push @FileList,$Inpath.$file if($file=~/\.pl/);
  21.  
  22.  
  23. }
  24. closedir(DIR);
  25.  
  26. foreach(@subdir) ##read from sub-folders
  27. {
  28.    next if($_ =~/^\.*$/);
  29.    opendir(SUB, "$Inpath/$_") or die "Can't open $Inpath: $!";
  30.    while( defined (my $subfile = readdir SUB) ) {
  31.     push @FileList,$Inpath."$_\\$subfile" if($subfile=~/\.pl/); }
  32.     closedir(SUB);
  33. }
  34.  
  35. $tempdir = $Inpath . "scripts\\temp\\";
  36. $zipfile = "scripts.zip";
  37. $zip = Archive::Zip->new();
  38.  
  39. foreach (@FileList) {
  40.     my $FileName =$_;
  41.     print "Adding File.... $FileName \n";
  42.  
  43.     $zip->addFile( $FileName )
  44.         or do {
  45.         warn "Can't add file $FileName, $!\n";
  46.  
  47.     }
  48. }
  49.  
  50. my $status = $zip->writeToFileNamed($OutPath.$zipfile);
  51.  
  52. if ($status == AZ_OK) {
  53.     print "Zipping completed successfully\n";
  54.  
  55. } else {
  56.     print "Zipping completed with errors\n";
  57.  
  58. }
  59.  
Mar 18 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.