The File::Find and related solutions provided are the best answers to getting the task done.

However, so you understand the mechanics of directory recursion, as well as a bit of codestyle cleanup, I went ahead and modified your snippet to do what you ask without File::Find.

Update: Moved the report file open/close to the main function and corrected one of the comments.

Examine at your leisure:

#!/usr/bin/perl use strict; my $DIRSEP = '\\'; # FIXME This is for DOS/Windows. Make this mor +e portable. open(OUT, '>fileout.txt') or die "Couldn't open the " . "fileout.txt file for writing.\n"; &CheckFileAccess('.'); close(OUT); exit; sub CheckFileAccess() { my $BaseDir = $_[0]; opendir(HERE, $BaseDir); my @AllFiles = readdir(HERE); closedir(HERE); # Pass 1 for files, Pass 2 for directory recursion for (my $curpas = 0; $curpas < 2; $curpas++) { foreach my $Name (@AllFiles) { my $RelName = $BaseDir . $DIRSEP . $Name; if (!$curpas) { # Process Files only; if directory skip to next filena +me if(-d $RelName) { next } my $FileAcc = sprintf('%3d', -A $RelName); # Check for files that have been accessed in the last +10 days if ($FileAcc <= 10) { print "$RelName "; print "File was last Accessed ", $FileAcc, " days +ago\n"; # ------------------------------------------------ +--------- # DANGER printf could have produced interesting a +nd # unexpected results here, so I changed it to prin +t. # ------------------------------------------------ +--------- # printf OUT $Name . "\n"; print OUT $RelName . "\n"; } else { print $RelName, " File was Accessed more than 10 d +ays ago\n"; } } else { # Process Directories only if(-d $RelName) { # Skip danger directories if ($Name eq '.') { next } if ($Name eq '..') { next } # Good to go -- prepare for recursive call &CheckFileAccess($RelName); } } } } }

In reply to Re: Recursive Subdirectories by marinersk
in thread Recursive Subdirectories by pildor

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.