I have created a script that traverses a file system (starting at your currect working directory) and lists all files and directories it finds. I need a way to show when each node (file or directory) was processed.

My script:

#!/usr/bin/perl -w use strict; use Cwd; # Perl script to traverse a given directory and print when each direct +ory or file is discovered. # This starts from the user's current working directory. my $dir = cwd(); my $discovered = 0; my $processed = 0; chomp (my $current = `pwd`); sub process { $dir = shift; foreach (<$dir/*>) { my $file = $_; $file =~ s/$current\///g; next if (-l $_); if (-f $_) { print "Filename: $file\nDiscovered: $discovered\nProcessed: $pro +cessed\n\n"; } if (-d $_) { print "Directory name: $file\nDiscovered: $discovered\nProcessed +: $processed\n\n"; process($_); } } } process($dir);
If I were to run this against the directory layout

example_dir {file_one.txt file_two.txt next_dir {file_three.txt}} (file_one.txt and file_two.txt are inside example_dir, and next_dir is inside example_dir... file_three is inside next_dir, sorry if it's not clear)

I need to output to look like:
Filename: file_one.txt Discovered: 1 Processed: 2 Filename: file_two.txt Discovered: 3 Processed: 4 Directory name: next_dir Discovered: 5 Processed: 8 Filename: file_three.txt Discovered: 6 Processed: 7

I'm trying to print when each node is discovered and processed in the stack. Can anyone help?


In reply to Help with node discovering and processing by cspctec

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.