cspctec has asked for the wisdom of the Perl Monks concerning the following question:
My script:
If I were to run this against the directory layout#!/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);
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with node discovering and processing
by AppleFritter (Vicar) on Jul 05, 2014 at 09:31 UTC | |
|
Re: Help with node discovering and processing
by Laurent_R (Canon) on Jul 05, 2014 at 07:25 UTC | |
|
Re: Help with node discovering and processing
by 2teez (Vicar) on Jul 05, 2014 at 16:41 UTC |