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

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

Replies are listed 'Best First'.
Re: Delete Files
by steves (Curate) on Feb 03, 2003 at 19:30 UTC

    Your if statement that's checking the modification times is only being applied to the print statement. The unlink is running against all files -- no conditional there.

Re: Delete Files
by Cabrion (Friar) on Feb 04, 2003 at 00:53 UTC
    Your use of chomp is unnecessary. Read perldoc -f chomp, as it applies to stripping line endings from text files. readdir() doesn't return files names with a CR/LF on the end.

    Your use of stat() is also unnecessary. You aren't capturing it's return value

    To clarify the previous response:

    if (-f $filepath) says "if $filepath is a file then do everything in the following block of code"

    You probably want:

    if (-f $filepath && (-M $filepath > $daysToPurge))
    Which says "if $filepath is a file AND it's mod time is more than 3 days agao . . ."

    Can't say why it worked on the first machine. It shouldn't have.

    And just a tidbit that you might find helpful. It has nothing to do with perl but applies to the art of SysAdmin.

    Naming files as somefile-2003-Jan... isn't going to sort nicely when you do a DIR command or view the directory in explorer. That's because $somefile-2003-Feb.. will appear before the Jan file, etc. It's better to use numeric months for that reason. Perhaps I'm just anal. . .

Re: Delete Files
by runrig (Abbot) on Feb 04, 2003 at 01:28 UTC
    In addition to the other comments here, you can save yourself one stat per file if you use the cached "_" file, like this:
    for my $filename (@files) { my $filepath = "$dir$filename"; stat($filepath); if (-f _ and -M _ > $daysToPurge) { print LOGF "$filepath will be purged\n"; unlink $filepath or print ERRLOG "Can't remove $filepath: $!"; } }
      Thank you all for your response. I used your information and it works great. I also changed the naming convention as it was pointed out it would not sort well.