perl - Check to see if a file has todays data inside it. If so, exit. Otherwise add data -
i have script runs , updates monthly csv file info daily csv file. trying add, check, if current days date in file in column 'a' means info has been copied on daily file, nil , exit. if today's date not in column 'a' re-create info , append monthly file. have far works until seek , append data. appends if today's date present.
i can't check when file lastly accessed other people access file.
#!/usr/local/bin/perl #use warnings; utilize strict; utilize posix 'mktime'; utilize posix 'strftime'; @dateparts = localtime (); ($day, $month, $year) = @dateparts[3 .. 5]; $year += 1900; $month += 1; open ( output, '+<', "c:\\temp\\monthly.csv" ) or die "cannot open file $!\n"; while ( defined ( $line = <output> ) ) { chomp $line; ($date) = split ',', $line; if ( $date eq "$day $month $year" ) { close output; exit print "\ file has been updated today"; } elsif ( $date ne "$day $month $year" ) { #it works fine until seek #open (input, "c:\\temp\\daily.csv") # or die "cannot open file $!\n"; #open (output, '>>', "c:\\temp\\monthly.csv") # or die "cannot open file $!\n"; #<input>; #while ( <input> ) { # print output; #} } } #close input; #close output;
there's 1 possible way else block execute, , that's if conditional associated first part of if block false.
and way can false if $date not equal "$day $month $year".
put print statement before if statement, looks this:
print "\$date ($date)\n", "\$day \$month \$year ($day $month $year)\n"; when line executes, you'll able ascertain mismatch is. must case don't match. why don't match, cannot sure, since haven't seen input data. armed information, should able deduce you're going wrong.
several other issues: elsif clause else, without conditional, because logic dictates if $date eq "$day $month $year" false, $date ne "$day $month $year" has true.
the next issue phone call exit terminate programme reached, making next line, "print "\ file has been updated today";" unreachable code.
another issue stands out you're opening monthly.csv file read/write, you're calling filehandle output, you're reading handle. time around writing monthly.csv, you've reopened file in append mode. may phone call first filehandle relating input, , open in read mode, not read/write.
perl
No comments:
Post a Comment