mariog has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I have to rename the file inside a zip archive as well as the zip archive itself. the zip file and folder is called ATP.xxxxxx.zip and a file inside the zip is called identically ATP.xxxxx and I need to rename both the zip and the file inside the zip and remove the "ATP."

I tried this code but it does not work
#!/usr/bin/perl -w use warnings; use strict; use Archive::Zip qw( :ERROR_CODES ); my @files; @files = <ATP.xxxxxx*.zip>; foreach my $file (@files){ my $zip = Archive::Zip->new(); $zip->read('$file') == AZ_OK or die "read error $!"; $file=~ s{\.[^.]+$}{}; my $m1 = $zip->memberNamed('$file'); $file=~s/^.{4}//s; $m1->fileName('$file'); $zip->overwriteAs('$file.zip') == AZ_OK or die "write error $!\n" }
the zip file cannot be read..

Replies are listed 'Best First'.
Re: rename zip files and folders inside zip files
by toolic (Bishop) on Aug 27, 2015 at 13:06 UTC
    Single quotes prevent variable interpolation. Get rid of them around $file everywhere:
    #!/usr/bin/perl -w use warnings; use strict; use Archive::Zip qw( :ERROR_CODES ); my @files; @files = <ATP.xxxxxx*.zip>; foreach my $file (@files){ my $zip = Archive::Zip->new(); $zip->read($file) == AZ_OK or die "read error $!"; $file=~ s{\.[^.]+$}{}; my $m1 = $zip->memberNamed($file); $file=~s/^.{4}//s; $m1->fileName($file); $zip->overwriteAs("$file.zip") == AZ_OK or die "write error $!\n" }
Re: rename zip files and folders inside zip files
by GotToBTru (Prior) on Aug 27, 2015 at 13:12 UTC

    So the program aborts on the 2nd line of the foreach loop? Have you checked permissions on the file(s)?

    Ugh. Missed the single quotes :) Glad not everybody is this obtuse.

    Dum Spiro Spero

      it seems to work. but I need to rename 1700 files but it crash after a while because of too many open files... how can I increase the amount of open files or limit the amount of open files? </p

      IO error: Can't open ATP.xxxxxxxxxx.zip : Too many open files at /usr/share/perl5/Archive/Zip/FileMember.pm line 40 Archive::Zip::FileMember::_openFile('Archive::Zip::ZipFileMemb +er=HASH(0x1ff1560)') called at /usr/share/perl5/Archive/Zip/FileMembe +r.pm line 30 Archive::Zip::FileMember::fh('Archive::Zip::ZipFileMember=HASH +(0x1ff1560)') called at /usr/share/perl5/Archive/Zip/ZipFileMember.pm + line 381 Archive::Zip::ZipFileMember::rewindData('Archive::Zip::ZipFile +Member=HASH(0x1ff1560)') called at /usr/share/perl5/Archive/Zip/Membe +r.pm line 1063 Archive::Zip::Member::_writeToFileHandle('Archive::Zip::ZipFil +eMember=HASH(0x1ff1560)', 'IO::File=GLOB(0x1ff7598)', 1, 0) called at + /usr/share/perl5/Archive/Zip/Archive.pm line 420 Archive::Zip::Archive::writeToFileHandle('Archive::Zip::Archiv +e=HASH(0x1ea7730)', 'IO::File=GLOB(0x1ff7598)') called at /usr/share/ +perl5/Archive/Zip/Archive.pm line 456 Archive::Zip::Archive::overwriteAs('Archive::Zip::Archive=HASH +(0x1ea7730)', 'ROUTINGS.D150313.T1600.zip') called at ./ziprenamer2.p +l line 16 Can't write to /tmp/k4YwCOwKhU.zip at ./ziprenamer2.pl line 16 write error Too many open files
        From the POD:
        endRead() Must be called to close files, etc.