Wierd issue with File::Find and directory

R

rishid

Hi,

My code works fine if the $root_dir = "." but say if I enter $root_dir
= "c:\\winnt" it won't work. If you uncomment and comment the other
line you will see the problem. I just cannot figure out that problem
at all.

#!/usr/bin/perl -w
use strict;
use File::Find;
use File::Spec;
use Data::Dumper;

my $size_key = "SIZE%:/_-SIZE";
my $recsize_key = "RECSIZE%:/_-RECSIZE";
my $file_key = "FILE%:/_-FILE";
my $recfile_key = "RECFILE%:/_-RECFILE";
my $subdir_key = "SUBDIR%:/_-SUBDIR";

### UNCOMMENT AND COMMENT OUT THE LINES TO SEE THE ISSUE
#my $root_dir = $ARGV[0]; # Default. Change as needed.
my $root_dir = ".";
#my $root_dir = "c:\\perl_files";
my $dir_tree = {}; # Reference to an empty hash

find(\&wanted, $root_dir);

print Dumper $dir_tree;
sub wanted
{
my $size = (stat $_)[7];
my @path_components = File::Spec->splitdir($File::Find::name);

my $filename = pop @path_components;

my $pointer = $dir_tree;

foreach my $component (@path_components)
{
$pointer->{$recsize_key} += $size;
if (-d $_) {
$pointer->{$subdir_key} += 1;
}
else {
$pointer->{$recfile_key} += 1;
}
# Move pointer to next directory and add size
#print "$component ";
$pointer = $pointer->{$component};
$pointer->{$size_key} += $size;
}
#print "\n";
if (-d $_) { $pointer->{$_} = { $size_key => $size }; }
}
 
T

Tad McClellan

it won't work.


What did you observe that led you to that conclusion?

What were you expecting it to do?

(this is a brand new thread but you haven't said what the
objective of the program is...
)

What was it doing instead?

Have you seen the Posting Guidelines that are posted here frequently?

If you uncomment and comment the other
line you will see the problem.

my $root_dir = ".";
#my $root_dir = "c:\\perl_files";


We can NOT see the problem because we cannot duplicate the
problem without setting up a directory structure first.

I'm not going to corrupt my filesystem just to find out
what you are talking about...
 
J

Jürgen Exner

Could have been any directory

What directory are you talking about? Sounds like you are replying to
someone/something, but without some quoted text it's impossible to tell what
you mean.
but I figured out the issue.

What issue are you talking about?

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top