I am trying to write code that will do the following:
1) look in the "c:\data" directory for all order files (ord.*)
2) look at each header record (ordertype = 'HM') and then look at the orderdates of each of these records.
3) if
all the dates in the file are Monday dates, move this file to c:\data\badorders, and move on to the next file. As soon as I hit a non-Monday date, the order should be moved to c:\data\goodorders
I have two ord.* test files. The following code is looking through the first file, but completely skips the second.
Code:
#!/usr/bin/perl
use strict;
use File::Copy;
use Time::Local;
my $goodpath = "c:\\data\\goodorders";
my $badpath = "c:\\data\\badorders";
my $starttime = localtime;
print "*** Starttime = $starttime ***\n\n";
if (-d "c:\\data\\") {
my $where = "c:\\data";
print "Directory $where exists...now continuing\n\n";
foreach my $orderfile (<c:\\data\\ord.*> ) {
print "Processing Order File: $orderfile\n\n";
open (FILEH, $orderfile) or die "Could not open order file: $orderfile : $!\n";
while (my $line = <FILEH>)
{
my $ordertype = substr($line, 0, 2);
if ($ordertype eq "HM") # only look at the dates on the header records
{
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 ) {
# We have found a date that is a Monday. We need to keep looking at the
# rest of the dates to see if the rest of the dates are Mondays
print "All dates in the file $orderfile are Mondays. Now moving it to the $badpath directory\n\n";
move("$orderfile", "$badpath") or die "Could not move the file $orderfile. Move failed: $!";
} else {
# We have found a date that is not a Monday. This file is OK. We need to move it to be processed
print "Not all dates in the file $orderfile are Mondays. Now moving it to the $goodpath directory\n\n";
move("$orderfile", "$goodpath") or die "Could not move the file $orderfile. Move failed: $!";
last; # bail if date is not Monday
}
}
}
close FILEH;
}
} else {
my $where = "c:\\data";
print "Directory $where does not exist...now exiting\n\n";
}
my $endtime = localtime;
print "*** Endtime = $endtime ***\n\n";