Ok, thanks, I wrote this based on your suggestions, and it seems to do what
I want. One further question, if you don't mind, how to format this so that
it prints sizes in megabytes, not in bits?
---code---
#!/bin/perl
use warnings;
use strict;
use File::Find;
my $dir = $ARGV[0];
die "You must supply a full directory path" unless (-e $dir && -d $dir);
opendir (DIR, $dir) or die "can't opendir $dir: $!";
my $total_size_of_files_in_dir;
while (defined(my $directory = readdir(DIR))) {
next if $directory =~ /^\.\.?$/;
next if -f $directory;
find(\&wanted, $dir);
print "The total size of the file in $directory is
$total_size_of_files_in_dir bytes\n";
}
sub wanted {
if (-f $_) {
$total_size_of_files_in_dir += -s;
}
}
---code---
-L
2009/3/4 Dermot <[email protected]>
> 2009/3/4 Lauri Nikkinen <[email protected]>:
> > Thank you for your post. That is quite there but not enough. See, I have
> > these directories and files in my C:\Perl\ folder
> >
> > Volume in drive C has no label.
> > Volume Serial Number is 248A-0894
> >
> > Directory of C:\Perl
> >
> > 04.03.2009 19:18 <DIR> .
> > 04.03.2009 19:18 <DIR> ..
> > 03.03.2009 21:41 <DIR> bin
> > 03.03.2009 21:24 <DIR> cpan
> > 03.03.2009 21:40 <DIR> eg
> > 03.03.2009 21:42 <DIR> etc
> > 03.03.2009 21:41 <DIR> html
> > 03.03.2009 21:41 <DIR> lib
> > 03.03.2009 21:40 <DIR> man
> > 03.03.2009 20:23 <DIR> OmatPerlit
> > 03.03.2009 22:09 225 Print_directory_sizes.pl
> > 01.10.2008 18:00 <DIR> site
> > 04.03.2009 19:18 0 text.txt
> > 2 File(s) 225 bytes
> > 11 Dir(s) 28ÿ409ÿ733ÿ120 bytes free
> >
> > And I would like to write to script which prints into STDOUT (=cmd
> screen)
> > all the directories in this folder (C:\Perl\) and and their size. So the
> out
> > put should look like this:
> >
> > Directory bin: size xxx megabytes
> > Directory cpan: size xxx megabytes
> > Directory eg: size xxx megabytes
> > Directory etc: size xxx megabytes
> > ...and so on
> >
> > Even better, if I could print out all the subdirectories also.
> >
> > -L
> > 2009/3/4 Dermot <[email protected]>
> >>
> >> 2009/3/3 Wagner, David --- Senior Programmer Analyst --- CFS
> >> <[email protected]>:
> >> >> -----Original Message-----
> >> >> From: [email protected]
> >> >> [mailto:[email protected]] On Behalf Of Lauri Nikkinen
> >> >> Sent: Tuesday, March 03, 2009 11:38
> >> >> To: Perl Beginners
> >> >> Subject: Printing directory sizes
> >> >>
> >> >> Hi,
> >> >>
> >> >> I'm trying to print directory sizes using script from
> >> >>
> >> >> http://coding.derkeiler.com/Archive/Perl/perl.beginners/2005-0
> >> >> 8/msg00693.html
> >> >>
> >> >> and when I try it from the cmd.exe
> >> >>
> >> >> C:\Perl>perl Print_directory_sizes.pl "C:/Temp"
> >> >>
> >> >> but I get an error message saying
> >> >>
> >> >> use of uninitialized value.... etc.
> >> >>
> >> >> Where is the problem? I'm using Win XP.
> >> >>
> >> >> ---code---
> >> >> #!/bin/perl
> >> >>
> >> >> use warnings;
> >> >> use strict;
> >> >> use File::Find::Rule;
> >> >>
> >> >> my $dir = $ARGV[0];
> >> >> my $size;
> >> >>
> >> >> find( sub { -f and ( $size += -s _ ) }, $dir );
> >> >> ---code---
> >> > I took the code and removed the ::Rule and left the other and
> it
> >> > ran fine, but did not print anything. So I add a print statement for
> the
> >> > $size and it worked without any error msgs,etc and it gve the right
> >> > values.
> >> >
> >> > What actually happens when you run? Not just the use of uni..
> >> > but all the output.
> >>
> >> I think what Lauri is after, is the accumulated total of all the files
> >> with a directory, something like this perhaps:
> >>
> >> #!/bin/perl
> >> use strict;
> >>
> >> use warnings;
> >>
> >> use File::Find;
> >>
> >> my $dir = shift;
> >>
> >> die "You must supply a full directory path" unless (-e $dir && -d
> $dir);
> >> my $total_size_of_files_in_dir;
> >>
> >> find(\&wanted, $dir);
> >>
> >> print "The total size of the file in $dir is
> >> $total_size_of_files_in_dir bytes\n";
> >>
> >> sub wanted {
> >> if (-f $_) {
> >> $total_size_of_files_in_dir += -s;
> >> }
> >> }
> >> A recursive example might be a better tool though.
> >> Dp.
>
>
> You seem to have changed the spec a bit.
>
> Perhaps you want the perl functions: opendir and readdir. The latter
> has an example of it's use. You can read how to use these function by
> type the command `perldoc -f readdir`
>
> Then you will want to accumilate the total for each file within a
> directory, use the -s switch as you did/saw in the earlier scripts.
> perldoc -f -X
>
> What I am afraid of doing here is all the work for you because I
> haven't seen an attempt by you to figure this out for yourself. If I
> recall from your original post, the first script you got off of the
> web. If you make an attempt with the functions mentioned above, I'll
> be glad to give you some guidence :)
> Dp.
>