Real Perl has asked for the wisdom of the Perl Monks concerning the following question:

Beloved,

When I write:
#!/usr/bin/perl -w use strict; use Tk; use File::stat; my $mw;#first window my $logl; #label for log my $logtf; #entry box for the log name my $path=""; #hol my $total=0; $mw = MainWindow -> new(); $logl = $mw ->Label (-text=> '\path', -font=>"Adobe 10")->pack; $logtf = $mw ->Entry (-textvariable=> \$path, width=>25)->pack; my $okb =>$mw -> Button(-text=>'GO', -command => sub {size()})->pack(- +side =>'right'); MainLoop; sub size{ chdir $path; opendir DIR, $path; my @file = readdir DIR; closedir DIR; foreach my $file (@file) {$st = stat($file) or die "No $file: $!"; $total = ($st->size)/1048576;#converts from bytes to MB }#foreach print "$total MB for this directory or file"; }#size
and I give it a directory that has MB or GB of data, the numbers that are coming back are not correct.

Basically, I need to call a size method on some directory and collect that number and if necessary change it to be in MB. I will also need to do that with a list of directory.
I would be most greatful if you could give me some clues as to what is wrong and how I can fix it.
Thanks in advance,
Claire

Replies are listed 'Best First'.
Re: Finding the size of a directory
by osunderdog (Deacon) on Aug 06, 2005 at 00:12 UTC

    Are you trying to sum up the total? Rather than this:

    $total = ($st->size)/1048576;#converts from bytes to MB

    Do you want this?

    $total += ($st->size)/1048576;#converts from bytes to MB

    I would recommend collecting the total first and then dividing by 1M.

    Also, you should declare $total in the scope of the size function so that it will start at zero each time you press the size button.

    Update Added more comments

    Still Unemployed!

Re: Finding the size of a directory
by davidrw (Prior) on Aug 06, 2005 at 00:16 UTC
    your code won't compile ($st isn't declared).. How are the numbers you're getting not correct? But that aside, take a look at yesterday's thread Disk size manager in perl? .. Also, are you a linux system where you could just run du (in backticks, or open a pipe, etc)? not pure perl, but effective...
Re: Finding the size of a directory
by chester (Hermit) on Aug 06, 2005 at 02:11 UTC
    Is there any possibility the directory contains subdirectories? readdir will find those, but no files in them. readdir also finds '.' and '..', which it doesn't seem you take into account. stat returns true for directories, and it returns a filesize for directories which you may or may not want.

    If you want to recurse and only pick up files, perhaps this is a job for File::Find and friends, rather than readdir. There's also Win32::DirSize if you're on Windows.

      Hi,

      My program has two different modes. When the user in on the first mode, he can only enter one path (c:\mydirectory\file.txt or c:\directory). My job is to make perl get the size of what was entered.
      When the user is on the second mode, he not only gives me the path (c:\directory\), but in a textarea, I make him give me a bunch of directories which I will append to c:\directory\ so what I give Perl looks like c:\directory\thedirectoryfromtextarea. What is in the textarea can be files or directories.
      What do you recommand I use to get the size that would cover both modes?
      As a side note, I tried installing Win32::DirSize and it is telling me it cannot found it but I need to do a "broader search" and when I type search Win32::DirSize it says there is no match for Win32::DirSize. I tried downloading it from activeperl, but it is not available either. I don't know what to do!
      Thanks in advance for your help,
      Claire