Michael J. Kazin Michael J. Kazin 7e2a2bec3feb298cf4ec4b15e0884354bb94ab06 Kevin R. Schmidt e4ad0654f93121d87424b6e3bce45cc9234b9e38 Purple over Orange: Simple problem + Wzzip frustration + Perl frustration = Solution 1 hour later

Tuesday, March 17, 2009

Simple problem + Wzzip frustration + Perl frustration = Solution 1 hour later

I'm not sure why Wzzip, Winzip's Command Line extentsion tool, thinks that deleting directories is a good idea.  I suppose pent-up frustrations are involved - it's a menial job being a compression tool and I'm not even letting it show off its mad compression skills.

Some background:  I'm working on a data transfer project and there's a COTS tool to move data from computer A to computer B.  Problem is, nobody thought to check if the COTS tool can handle large volumes of small files.  LARGE.  Think 10k files a minute.  So I wrote a tiny Perl script to simply transform lots of files into little ones.  Think tar(1), when it isn't available, which in a Windows shop means Winzip.

Anyway, I initially wrote the script and it worked great.  The only problem was that after moving files into the zip file Wzzip would delete the source directory, which made the data input mechanism quite unhappy.  The obvious solution was to simply not let it know it was working on a directory, which should be easy right?  Just change the working directory.
One would think that would be easy, but it took a bunch of reading on Cwd(), chdir(), exec(), system(), backticks to still get nowhere.  When all else fails, reverr to brute-force trial and error on a command prompt and a 3-line file.  Five minutes was all it took to get Perl doing what I wanted.  

So without further ado, here you go.  The two important lines are at the bottom.

      # Build a timestamp (yyyymmddHHMMSS)
      my @e=localtime ;
      my $cstamp = sprintf "%4d%02d%02d%02d%02d%02d", $e[5]+1900,$e[4]+1,$e[3],$e[2],$e[1],$e[0];

      # Build zip filename
      my $zipname = "$OUTDIR\\$prelastpath\\$lastpath\\$prelastpath-$lastpath$cstamp.zip";

      # Package all files in the current path
      #   -e0 - no compression (perhaps tar-like?)
      #   -m  - move files to the Zip file
      print "Packaging:  $zipname\n";

chdir("$SOURCEDIR\\$prelastpath\\$lastpath"); 
      my $res = `"\\Program Files\\Winzip\\WZZIP.EXE" -m -e0 $zipname *.dat`;

Labels:

0 Comments:

Post a Comment

<< Home