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

Hi All,
I am using the below code to find the size of the folder. But i have lot of folders and it's taking more time. Any other method to achieve the same. i tried to install win32::Dirsize but it's not installing properlt thru ppm.

use File::Find; my $size; find(sub{ -f and ( $size += -s ) }, $data ); $size = sprintf("%.02f",$size / 1024 / 1024); if ($size > 2048) { print "Directory $data contains $size MB\n"; }

Replies are listed 'Best First'.
Re: folder size
by cdarke (Prior) on Apr 19, 2010 at 11:30 UTC
    Looking at the XS code for Win32::Dirsize it looks like it is doing more or less the same thing as you are. OK, so it is using C, but most of your work is being done at a low-level as well, so I'm not sure that you would see much of a performance improvement.

    If you right-click on a directory name on Windows Explorer and select 'Properties' you will find that it takes quite a while to get the "size on disk" the first time you do it. Is your Perl substantially slower than Windows Explorer?
Re: folder size
by Marshall (Canon) on Apr 19, 2010 at 13:16 UTC
    One thing that could possibly be tried is using Microsofts diruse command. This is in Windows XP Service Pack 2 Support Tools

    c:\temp>diruse <foldername> /m
    should give size in MB

    I've never used this so I buyer beware! I suspect that cdarke is right in that this is just an expensive thing to calculate and it takes awhile.

      For Windows you can use this nice freeware: Folder Size It is really good and provides also bar and pie chart visualization.
Re: folder size
by Khen1950fx (Canon) on Apr 19, 2010 at 20:54 UTC
    Try this:
    #!/usr/bin/perl use strict; use warnings; use File::Size; my $dir = shift @ARGV; $dir = File::Size->new( dir => $dir, blocksize => 2048, humanreadable => 1 ); print $dir->getsize(), "\n";
Re: folder size
by Discipulus (Canon) on Apr 20, 2010 at 09:09 UTC
    more than one way.. always..
    sub GetDimension { my $root = shift; return 'NA' unless -e $root; my $dimension='0'; my $fs = Win32::OLE->CreateObject('Scripting.FileSystemObject'); my $folder = $fs->GetFolder($root); $dimension = $folder->size(); $fs = undef; return $dimensione; }
    HTH Lor*

      Another way to get Size of files in folder(s).
      Hi Sandor

      use 5.006;
      use strict;
      use warnings;
      use File::Glob ':glob';
      
      if (scalar @ARGV== 0) {die "folderSize.pl \t-s <foldername> <foldername> \n\t\t -s  scan subfolders\n"};
      my $foldersize=0;
      my @folders=@ARGV;
      my $subfolder="";
      
      if ($ARGV[0] eq "-s") {$subfolder=shift @ARGV; @folders=@ARGV;}
      print "Folders @folders\n";
      
      for my $folder (@folders){
           printf STDERR "Size of folders=%10d\r" ,$foldersize;
           unless (-e $folder) {print STDERR "Folder does not exist $folder\n"; next}
           $folder=~s/\/$//;
           $folder=~s/\\$//;
           my @entries=glob("$folder/*");
           for (@entries){
      	 if (-d){
      #	         print STDOUT "folder $_\n" ;
      	          push @folders, $_ if $subfolder;
      	         }
      	 else   {
      #	         print STDOUT "file : $_ \tsize:".(stat)7."\n";
      	         $foldersize+=(stat)7;
               }
            }
       }
      
      print "Size of files in folder(s) @ARGV is =$foldersize bytes";
      
      
        USAGE :folderSize.pl [-s] <foldername> [<foldername] To get the file size use: (stat)[7] in the previous script.(Jul 18, 2010 at 11:54 UTC) Sandor