If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Need help with looping

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-06, 14:32
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Sep 2005
Posts: 220
Need help with looping

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";
Reply With Quote
  #2 (permalink)  
Old 08-14-06, 15:52
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Sep 2005
Posts: 220
I almost have it now that I've hammered at it for a while. The only thing I can't seem to get now is to move the file to the \badorders directory of all the dates are Monday. I've tried putting the "move" statement in various places but none of them work.

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. Directory check was successful.\n\n";

    # find the number of files we are to process
    my @count = (<c:\\data\\ord.*>);
    my $plural  = (scalar(@count = (<c:\\data\\ord.*>)) == 1) ? "" : "s";
    print "Processing ", scalar @count, " order file$plural.\n";

    foreach my $orderfile (<c:\\data\\ord.*> ) {

        print "\nProcessing Order File: $orderfile\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 $ordernum = substr($line, 3, 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 ) {
              	    print "  Order Number =  $ordernum   Order Date = $date_str is a Monday\n";
	            # 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
                    next;  # move onto the next record and look at it
	      } else {
              	    print "  Order Number =  $ordernum   Order Date = $date_str is not a Monday. This file is OK to move and process.\n";
	            # We have found a date that is not a Monday. This file is OK. We need to move it to be processed
        	    close FILEH;
                    move("$orderfile", "$goodpath") or die "Could not move the file $orderfile. Move failed: $!";
	            last;  # bail out of the loop 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 "\n*** Endtime = $endtime ***\n\n";
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On