#!/usr/bin/perl use warnings; use strict; use Date::Calc qw(:all); my $PRINTFILE_DIRECTORY = "/home/lprjob1/filest"; # Directories to work in #my @dirs = ('dpi', 'dsc', 'dscpprod'); my @dirs = ('dscpprod'); my $dir; my ($year, $month, $day, $control_file, $pday, $pdatestr); # Form date string with arguement or default to today. my $dateform; if (scalar(@ARGV) eq 1 && $ARGV[0] =~ /\d\d\d\d-\d\d-\d\d/) { $dateform = $ARGV[0]; } else { ($year, $month, $day) = Today(); $dateform = sprintf("%d-%02d-%02d",$year,$month,$day); # Create current previous day's date string for later use # This will be needed for the control file. $pday = $day - 1; $year =~ s/^\d{2}(\d{2})/$1/; $pdatestr = sprintf("%02d%02d%02d",$year,$month,$pday); $control_file = "CONTROL_$pdatestr.txt"; } foreach (@dirs){ $dir = $_; print "Should $dir be processed?\n"; #If the control file does not exist then skip next unless ( -e "$PRINTFILE_DIRECTORY/$dir.$dateform/$control_file" ); print "Yes, $dir should be processed?\n"; # Unzip files gzip('unzip', "$PRINTFILE_DIRECTORY/$dir.$dateform"); # Zip files again gzip('zip', "$PRINTFILE_DIRECTORY/$dir.$dateform"); } sub gzip { my $cmd = shift; my $dir = shift; my $file; my $fullfile; print "$cmd files in $dir...\n"; # Make sure command is secure and predictable. if ( $cmd eq 'zip' ){ $cmd = 'gzip'; } elsif ( $cmd eq 'unzip' ){ $cmd = 'gunzip'; } else { warn "zip or unzip command not given $!"; } opendir DIR, $dir or warn "Cannot open $dir $!"; while ( defined ( $file = readdir(DIR) ) ){ $fullfile = "$dir/$file"; if ( !-f $fullfile || $file =~ m/\.txt$/ || $file !~ m/^[\w-]+(\.[\w-]+)*$/ ){ print "skipping $fullfile\n"; next; } #print "$cmd $fullfile"; system ( "$cmd $fullfile") == 0 or warn "Cannot $cmd $fullfile : $!"; } closedir DIR; }