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

HEllo Experts,

I am trying to get size of all the folders in a network drive on windows and using following codes but no success.

use strict; use warnings; use File::stat; my @scoll; #my $path = "c:\\temp\\"; my $path = "\\\\domain.local\\users\\"; opendir DIR, $path or die $!; my @alldir = readdir DIR; closedir DIR; foreach my $eachdir (@alldir) { my $cpath = $path . $eachdir; #print $cpath; my $sEachDir = stat($cpath)->[7]; push (@scoll, $sEachDir); } print "@scoll\n";

$eachdir can print all the subfolders available in network drive, however size printed will be 0. when i tried with local drive for example c:\temp, it shows the correct size of folders but not when setting path to a network location

Can you suggest something?

-swissknife-

Replies are listed 'Best First'.
Re: size of all subfolder from a network location
by Mr. Muskrat (Canon) on Jun 05, 2014 at 20:44 UTC

    How about something like this? It is mostly a copy/paste from the Win32::DirSize synopsis. I have tested this on my Windows 7 laptop after installing it with ppm install --force http://www.bribes.org/perl/ppm/Win32-DirSize.ppd (a cpan install of the module failed).

    use strict; use warnings; use Win32::DirSize; my $path = "\\\\domain.local\\users"; y $result = dir_size( $path, my $DirInfo ); if ( $result == DS_RESULT_OK ) { print "Dir size is $DirInfo->{DirSize} bytes\n"; print "Dir size on disk is $DirInfo->{DirSizeOnDisk} bytes\n"; } if ( @{ $DirInfo->{Errors} } ) { for my $Error ( @{ $DirInfo->{Errors} } ) { printf( "Error #%d at %s\n", $Error->{ErrCode}, $Error->{Locat +ion} ); } }
Re: size of all subfolder from a network location
by poj (Abbot) on Jun 05, 2014 at 17:57 UTC
    Can you suggest something?
    #!perl use strict; my $path = "\\\\domain.local\\users\\"; for ( qx(dir /S $path) ){ print $_ if /Directory of|File\(s\)/; }
    poj
Re: size of all subfolder from a network location
by locked_user sundialsvc4 (Abbot) on Jun 05, 2014 at 18:12 UTC

    stat() might not work for a network file and some parts of it might not be implemented because it could be a very expensive operation, and might not succeed and/or be accurate (e.g. for a curently-open file).   Network file systems vary considerably on that point.   Although the suggestion to actually execute a dir command might or might not be the best, the principle is a good one:   use the directory entry, and hope that it is reasonably up to date.   On other systems, other commands (such as du on a Unix box) might be preferable ... and, this being Perl, I am quite sure that there’s already The Perfect CPAN Module out there.   In any case, rely on the file-size information that is posted in the directory of that filesystem, one way or the other.   Trust that it will be close-enough ... and in any case that it can more-or-less be obtained locally instead of potentially far-away.