Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

OLE dir size and number of files and folders

by blackadder (Hermit)
on Oct 03, 2003 at 21:10 UTC ( [id://296394]=perlquestion: print w/replies, xml ) Need Help??

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

I have this code below, which obtains the size of a directory and the number of files and folders (I normally use it on dirs on remote machines). It works fine but it takes twice as long as doing the same thing manually. I suspect this is due to obtaining the size (that’s one), and then obtaining the number of files and folders (that’s two), which makes the script execute two operations thus taking twice as long. My question is; Is there a way that I can combine the two operations in one, so that the script can take just as long as doing the same manually Or a bit faster?

Thanks
use strict; use Win32::OLE qw[in with]; my $fs = Win32::OLE->CreateObject('Scripting.FileSystemObject'); my $fCount =0; my $sCount =0; my $size = 0; my $d = $fs->GetFolder("$ARGV[0]"); my @folders = $fs->GetFolder("$ARGV[0]" ); eval { $size = $d->size( ); while( @folders ) { my $folder = pop @folders; $fCount += $folder->Files->Count; $sCount += $folder->SubFolders->Count; for my $subFolder ( in $folder->SubFolders ) { $fCount += $subFolder->Files->Count; push @folders, $_ for in $subFolder->SubFolders ; $sCount += $subFolder->SubFolders->Count; } } my $size_gb = ($size/1024/1024); print "Size: ".$size_gb."Mb ($size byte), Files: $fCount, folders: + $sCount"; };

Replies are listed 'Best First'.
Re: OLE dir size and number of files and folders
by particle (Vicar) on Oct 04, 2003 at 02:53 UTC

    drop ole and use File::Find::Rule (a fine module, and platform independent!)

    #!/usr/bin/perl use strict; use warnings; use File::Find::Rule (); die 'usage: dir_size.pl *directory*' unless 1 == @ARGV; my( $size, $files, $folders ); my $rule= File::Find::Rule->start( @ARGV ); while( my $item= $rule->match ) { next if $ARGV[0] eq $item; $size+= -s $item; -f $item and $files++; -d $item and $folders++; } printf "Size: %fMb (%d byte), Files: %d, folders: %d\n" => $size / 1024**2, $size, $files, $folders;

    ~Particle *accelerates*

      The code works fine on local dirs, however when assigning a remote folder to ARGV, I get the following error;
      C:\Scripts>size_file_test3.pl //srvA/g$/Eng_Support/Data_Removal Use of uninitialized value in printf at C:\Scripts\size_file_test3.pl +line 20. Size: 1.414590Mb (1483305 byte), Files: 3, folders:
      And the value of the folders is incorrect (always zero). I would appreciate it if you could guide me to fixing this problem now I am close to solving my issues.

      Thanks for this Mr Particle.

      Blackadder

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://296394]
Approved by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found