Monks, I've been working on a script that will use File::Find to traverse a directory tree, deleting files older than 30 days since last modified. The traversal works fine, and the deletions work to a certain extent, but it just seems like I'm missing about half of the files that need to be deleted... Here is most of the code, please provide suggestions and criticisms.
use File::Find; my @time = localtime(); $time[5] += 1900; $time[4]++; my $curmonth = $time[4]; my $today = $time[3]; my $three = $curmonth - 0; my $year = $time[5]; my @filearray; my ($dirname, $outputfile) = <@ARGV>; chomp($dirname); chomp($outputfile); print "Beginning... "; my $filecounter = 0; open OUTPUT, ">$outputfile"; print "\nFrom the Directory: ", $dirname; print OUTPUT "\nFrom the Directory: ", $dirname, "\n"; opendir DIR, $dirname; chdir($dirname); my @directories = readdir (DIR); find(\&wanted, @directories); sub wanted { -l && !-e && print "bad link: $File::Find::name\n"; my $filepass = $File::Find::name; my $sendcheck = $File::Find::name; if ($filepass eq ".."){ print "\n\n\tFrom $dirname -- Files/Directories Scanned: ",$filecounter, "\n"; close OUTPUT; exit;}else{ &CheckMods($sendcheck); } } sub CheckMods { my $file = shift; my $tempvar = $_; my @info = stat($tempvar); my $last_access = $info[8]; my $last_modification = $info[9]; $filecounter++; if (@info){ my @times = (localtime($last_modification)); $times[4]++; $times[5] += 1900; $notify =(" Date: "."$times[4]/$times[3]/$times[5]"); print "Checking folder... "; if ($times[5] < $year){ push(@filearray, $_); &delete(); }else{ if ($year = $times[5]){ if ($three > $times[4]){ push(@filearray,$file); &delete(); } } } } else { print OUTPUT "\n"; } return; } sub delete{ foreach (@filearray){ print OUTPUT "Deleting the following file: $_\n"; print "Deleting the following file: $_\n"; unlink || warn "Can not delete file $_: $!\n"; }return; } close OUTPUT;
UPDATE: When I run the script, it gives me the following message: "Cannot delete path/filename No such file or directory" It does successfully delete the appropriate files in the home directory, so it seems that it may be a traversal issue.

In reply to Deleting Files in 2000 by Zenkyoki

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.