Thanks. I modified the script again based on your suggestions. E.g. from this script I get
The total size of the file in etc is 15712.35 Kb The total size of the file in etc is 15.34 Mb and when I check this from Win XP Explorer (folder properties) window I get Size: 372 KB (380 928 bytes) What is the reason for this difference? -L ---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 @directories = grep {! /^\./ if -d} readdir(DIR); my $total_size_of_files_in_dir; foreach my $dir (@directories) { find(\&wanted, $dir); ### Not sure how that worked as you called it $directory print "The total size of the file in $dir is " . sprintf("%.2f Kb", ($total_size_of_files_in_dir * 0.0009765625)) . "\n"; print "The total size of the file in $dir is " . sprintf("%.2f Mb", ($total_size_of_files_in_dir * 9.5367431641e-7)) . "\n" } sub wanted { if (-f $_) { $total_size_of_files_in_dir += -s; } } ---code--- 2009/3/5 Dermot <paik...@googlemail.com> > 2009/3/4 Lauri Nikkinen <lauri.nikki...@iki.fi>: > > 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? > > > I tend to use this for kilobytes > sprintf("%.2f Kb", ($size_in_bytes * 0.0009765625)); > and this for megabytes > sprintf("%.2f Mb", ($size_in_bytes * 9.5367431641e-7)); > > This rounds to the second decimal place. See perldoc -f sprintf for > details. > > > > ---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: $!"; > > # This will omit any hidden directories (beginning with a dot) as well > as '..' and '.' > # which save you a couple of lines below. See the example in perldoc -f > readdir > my @directories = grep {! /^\./ -d } readdir(DIR) > > > my $total_size_of_files_in_dir; > > foreach my $dir (@directories) { > > find(\&wanted, $dir); ### Not sure how that worked as you > called it $directory > 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; > > } > > } > > I haven't tested the above. It's just my initial thoughts on what you > presented. I hope that helps. Stay tuned as I have some questions of > my own that might help you. > Good luck, > Dp. > > > > > ---code--- > > > > -L > > > > 2009/3/4 Dermot <paik...@googlemail.com> > >> > >> 2009/3/4 Lauri Nikkinen <lauri.nikki...@iki.fi>: > >> > 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 <paik...@googlemail.com> > >> >> > >> >> 2009/3/3 Wagner, David --- Senior Programmer Analyst --- CFS > >> >> <david.wag...@freight.fedex.com>: > >> >> >> -----Original Message----- > >> >> >> From: lauri.nikki...@gmail.com > >> >> >> [mailto:lauri.nikki...@gmail.com] 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. > > > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >