Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help on traversing directories and files

Status
Not open for further replies.

slok

Programmer
Jul 2, 1999
108
SG
I'm working on a script that will traverse directories
(either given from command-line arguments or default if no arguments).

It will list the top disk usage users. I do not intend to
make system calls like "du" etc... but rather do it entirely in Perl.

I have some of the validations up and an idea of the control
flow... but I have problems coding the sub routine for
TRAVERSE works.

Any other comments on my code is also welcomed.

Usage of the script is myscript.pl -N path1 ... path-X
- where N is a numeric number for number of users
if nothing is specified, it will default to 10 users

- path1 ... pathX..
it can potentially take in "infinite" numbers of paths.
paths can be absolute or relative.

Please see my code as follows:

====
#!/usr/bin/perl -w
#
use File::Find;
use strict;


my $path = "."; # default path
my $num = 10; # -n default number of users
my $num_flag = 0;
my $count = 0;
my @paths ; # array to hold paths
my $var = 0;
my %inodes = (); # to track where we've been
my %blocks_of_users = ();
my $num_paths = 0;

$main::size_value = 0;


foreach my $argv (@ARGV) {

if ( $argv =~ m/^-/ ) {
$num_flag++;
print "num_flag" . $num_flag . "\n";
if ($num_flag > 1) {
print STDERR "Cannot have more than one entry for number of users\n";
usage_exit(1);
}
$num = $argv;
print $num . "\n";
print("Matches dashes\n");
}

if ( $argv =~ m/^\// ) {
#@paths[$num_paths] = $argv;
$paths[$num_paths] = $argv;
$num_paths++;
print("Matches slashes" . $num_paths . "\n");
}
}

# set default path if no path argument is supplied
if ($num_paths == 0) {
@paths = <$path/*>;
}


# testing only, to see what goes inside @paths
foreach (@paths) {
print &quot;printing paths&quot; . $_ . &quot;\n&quot;;
}



# testing only...
foreach my $dir(@paths) {
print (&quot;$dir\n&quot;);
}


my $filesize = 0;



# I have trouble here..!!!
sub traverse ($&) {
my $dir = shift;
my $wanted = shift;

find($wanted, &quot;$dir&quot;);

};


# seeems to be working..
sub getsize () {
my $size = 0;
$size = -s File::Find::name;
$main::size_value += $size;

print sprintf(&quot;%d&quot;, (-s $File::Find::name)), &quot;\t$File::Find::name \n&quot;;
}


sub usage_exit {
print STDERR &quot;Usage [-n] [path 1..] [path..n]\n&quot;;
exit(@_);
}


traverse(&quot;$ARGV[1]&quot;, \&getsize);
print &quot;Total size: [&quot;, sprintf(&quot;%d&quot;, ($main::size_value)),&quot;]\n&quot;;
 
Have a look at the File::Recurse module, you can install it using PPM. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi Mike,

we can't use modules that is not part of the standard package.

is there any pointers on how I can work on Traverse routine
without the use of other modules.

thanks
 
the module itself has the code you would need to do what you need to do. if you can't 'use' it, you can just cut and paste the needed subroutine(s) in. they're pretty well made, so shouldn't be too hard to incorporate. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top