I am a rookie with Perl so I am sure what I am trying to do can be done easier another way. I created a script to delete files from an Windows 2000 server. It worked fine but I moved this code from one machine to another Windows 2000 system and it does not work the same. I have the same version of Perl running on both machines. What is happening is it is deleting all the files in the folder/directory. The following is my code:
use File::stat; my ($logfile, $oldlogfile, @logfile); my ($date, $sec, $min, $hour, $mday, $mon, $year); my(@months) = 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','O +ct','Nov','Dec'); $archivedirin = "F:/data/archiveinbound/"; #Directory on the local mac +hine where inbound data to purge resides. $archivedirout = "F:/data/archiveoutbound/"; #Directory on the local m +achine where inbound data to purge resides. $logdir = "F:/PerlScripts/Logs/"; #Directory on the local machine whe +re logs will be placed from purge. $daysToPurge = 2; # name logfile with current date ($sec, $min, $hour, $mday, $mon, $year) = localtime(time); $year += 1900; $logfile = sprintf "purgelog-%4d-$months[$mon]-%02d.log", $year,$mday; $errorlog = sprintf "errorlog-%4d-$months[$mon]-%02d.log", $year,$mday; $date = localtime; # open log message files open(LOGF, ">>$logdir$logfile") or die "can't open: $logfile\n"; print LOGF "- - $date files that will be removed from archiveinbound +- - \n"; open(ERRLOG,">$logdir$errorlog") or die "can't open: $logfile\n"; print ERRLOG "- - errors from removal of files from archiveinbound on + $date\n"; # List inbound archive directory opendir(DIR,$archivedirin)or die "can't opendir: $archivedirin\n"; @filename = readdir(DIR); closedir DIR; foreach $filename(@filename){ chomp $filename; my $filepath = "$archivedirin$filename"; stat($filepath); if(-f $filepath){ print LOGF "$filename is older then $daysToPurge days and wil +l be purged.\n" if -M $filepath > $daysToPurge; unlink "$filepath" or print ERRLOG "can not remove file $file +name from $archivedirin \n"; } } # end of purge of inbound archive directory print LOGF "- - end of archiveinbound purge $date - - \n"; print ERRLOG "- - end of archiveinbound purge $date - - \n"; # List outbound archive directory opendir(DIR,$archivedirout)or die "can't opendir: $archivedirout\n"; @filename = readdir(DIR); closedir DIR; print LOGF "- - $date files that will be removed from archiveoutbound +- - \n"; print ERRLOG "- - errors from removal of files from archiveoutbound on + $date\n"; foreach $filename(@filename){ chomp $filename; my $filepath = "$archivedirout$filename"; if(-f $filepath){ print LOGF "$filename is older then $daysToPurge days and wil +l be purged.\n" if -M $filepath > $daysToPurge; unlink "$filepath" or print ERRLOG "can not remove file $file +name from $archivedirout\n"; } }
Thank you, Mike

In reply to Delete Files by windy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.