G'day justin423,

Here's how you could do this in a single pass using only core features. I've included verbose output so you can see what's going on: you may not want some, or any, of this. In production, the $dir and $re values would be supplied via arguments, options, config file, or similar. Also, I've aimed for Perl v5.8; there are various improvements you could make with a more recent version — feel free to ask if you're unsure what those might be (stating, of course, the Perl version you're using).

#!/usr/bin/env perl use strict; use warnings; use autodie; use constant INDENT_PLUS => ' '; use File::Spec; use IO::Compress::Zip qw{zip $ZipError}; my $dir = '/home/ken/tmp/pm_11150213_zip_dirtree'; my $re = qr{00$}; comp00($dir, $re); sub comp00 { my ($dir, $re, $indent) = @_; $indent = '' unless defined $indent; print "$indent** Dir: '$dir'\n"; opendir(my $dh, $dir); for my $file (grep ! /^(?:\.|\.\.)$/, readdir $dh) { my $path = File::Spec::->catfile($dir, $file); print "$indent* Path: '$path'\n"; if (-f $path) { if ($path =~ $re) { eval { my $zip = "$path.zip"; zip $path, $zip or die $ZipError; print "${indent}Zipped: '$path'\n"; unlink $path; print "${indent}Deleted: '$path'\n"; 1; } or do { my $err_msg = $@; print "${indent}FAILED! $err_msg\n"; }; } else { print "${indent}Skipped: '$path' (doesn't match patter +n '$re')\n"; } } elsif (-d _) { print "${indent}Recurse: '$path' is a directory\n"; comp00($path, $re, $indent . INDENT_PLUS); } else { print "${indent}Skipped: '$path' (not plain file or direct +ory)\n"; } } closedir $dh; return; }

For testing, I created /home/ken/tmp/pm_11150213_zip_dirtree then ran the following to set up test directories and files. Sorry, I don't know the equivalent MS-DOS commands.

ken@titan ~/tmp/pm_11150213_zip_dirtree $ rm -rf a d ken@titan ~/tmp/pm_11150213_zip_dirtree $ mkdir -p a/b a/c d/e d/f/g ken@titan ~/tmp/pm_11150213_zip_dirtree $ for dir in a a/b a/c d d/e d/f d/f/g; do > $dir/x00; > $dir/y; > $di +r/z01; done

Here's the output from a sample run:

ken@titan ~/tmp/pm_11150213_zip_dirtree $ ./compress00.pl ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/.compress00.pl.swp' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/.compress00.pl.swp' (d +oesn't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/a' is a directory ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/a' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b' is a director +y ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/y' (doesn' +t match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/b/z01' (does +n't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c' is a director +y ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/y' (doesn' +t match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/c/z01' (does +n't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/a/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/y' (doesn't matc +h pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/a/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/a/z01' (doesn't ma +tch pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/compress00.pl' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/compress00.pl' (doesn' +t match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/d' is a directory ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/d' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e' is a director +y ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/y' (doesn' +t match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/e/z01' (does +n't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f' is a director +y ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g' Recurse: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g' is a di +rectory ** Dir: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/y' ( +doesn't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/g/z01' + (doesn't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/y' (doesn' +t match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/f/z01' (does +n't match pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/x00' Zipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/x00' Deleted: '/home/ken/tmp/pm_11150213_zip_dirtree/d/x00' * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/y' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/y' (doesn't matc +h pattern '(?^:00$)') * Path: '/home/ken/tmp/pm_11150213_zip_dirtree/d/z01' Skipped: '/home/ken/tmp/pm_11150213_zip_dirtree/d/z01' (doesn't ma +tch pattern '(?^:00$)')

— Ken


In reply to Re: Compressing files on an entire disk by kcott
in thread Compressing files on an entire disk by justin423

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.