skyler has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have created this program that parses a file and creates an output file; but I would like to rename the output file with the system date for example : 05272003.txt This will have to take place everyday. or al least from Monday thru Friday. The program will rename the ouput file with a date format. I'm new to the rename function so I will appreciate any ideas. Here is my code:
#!/usr/bin/perl -w use strict; my $infile = 'c:/RX_Q.008'; my $outfile = 'c:/testfile.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; my @finds = qw( 77403 77404 77406 77407 77408 77409 77411 77412 77413 +77414 77416 77418 ); my $finds_re = join '|', map { quotemeta }@finds; $finds_re = qr/$finds_re/; while(<IN>) { next if m/$finds_re/; print OUT; } close IN;

Replies are listed 'Best First'.
Re: rename output file with system date
by graff (Chancellor) on May 28, 2003 at 04:18 UTC
    You could try assigning the intended name to "$outfile" from the beginning:
    use strict; my $infile = 'c:/RZ_Q.008'; # is it the same input file every time?
    my $outfile = sprintf("%d.txt", time())
    my ( $yr, $mo, $dy ) = (localtime)[5,4,3]; my $outfile = sprintf( "%04d%02d%02d.txt",$yr+1900,$mo+1,$dy ); # (I tend to format date-as-filename so that it will be # easy to sort file names chronologically: YYYYMMDD) # you can add hours and minutes, if you like -- check # `perldoc -f localtime`...
    Now, the only issue is to control the scheduling for when the script gets executed (which is not a perl question, per se, unless you search for a "perl version of cron for ms-windows", or some such... but isn't there a scheduler utility that comes with ms-windows?)

    Apart from that, the compulsive golfer in me sees a few places where you could simplify/condense the code a bit. I should restrain myself -- maybe you're just showing a subset of the whole application, and premature optimizations are a sin anyway, of course. But I can't resist...

    # (assign $infile, $outfile as above, open IN, OUT as in your post, th +en: while (<IN>) { print OUT unless ( /774(0[346789]|1[123468])/ ); } # but if the set of numerics to exclude changes regularly, # you'll want to figure out how to convey the proper set # to the script on start-up, and construct a regex along the # lines shown in your original code.
      So you're a perl-golfer? So why comes you missed this instead of your while-loop?
      print OUT grep!/774(0[346-9]|1[1-468])/,<IN>;
      Thanks for replying but I'm trying to format a DATE which it gave an error becuse it is not numeric. do you have any ideas
        Oops -- sorry to have missed that clue in the original post -- I have updated/fixed the suggested code accordingly.