$allmondayflag = 1; foreach my $orderfile (<$instance_root/order_temp/ord.*>) { print "\nProcessing Order File: $orderfile\n"; open (FILEH, $orderfile) or die "Could not open order file: $orderfile : $!"; while (my $line = ) # loop as long as there are records in the file { my $ordertype = substr($line, 0, 2); # used to find the order header if ($ordertype eq "HM") # only look at the dates on the header records { my $ordernum = substr($line, 2, 7); my $date_str = substr($line, 64, 8); my ($year, $month, $day) = $date_str =~ m/(\d{4})(\d{2})(\d{2})/; my $time = timelocal("", "", "", $day, $month-1, $year); # For $month: Jan = 0, Feb = 1, etc. if ((localtime($time))[6] == 1 ) # the order is on a Monday { print " Order Number = $ordernum Order Date = $date_str is a Monday\n"; $allmondayflag = 1; next; # move onto the next record in the current file } else { # We have found an order date that is not a Monday $allmondayflag = 0; close FILEH; last; # bail out of the loop if date is not Monday and process the next order } } # end of Ordertype IF } # end of while loop # Decide where to move the file based on the allMondayFlag if ($allmondayflag) { print " Order Number = $ordernum has order dates that are all Mondays. This is a bad order file and is being moved to $badordpath\n"; move("$orderfile", $badordpath) or die "\nCould not move the file $orderfile. Move failed: $!"; } else { print " Order Number = $ordernum Order Date = $date_str is not a Monday.\n"; print " This file is valid and is being moved to $goodordpath\n"; move("$orderfile", $goodordpath) or die "Could not move the file $orderfile. Move failed: $!"; } close FILEH; # close file when we run out of lines to read } # end of ForEach loop } else { my $where = "$instance_root/order_temp"; print "Directory $where does not exist...now exiting\n\n"; }