On 10/23/22 21:56, ToddAndMargo via perl6-users wrote:
On 10/22/22 21:30, ToddAndMargo via perl6-users wrote:
Hi All,

Does Raku have a folder size command (including sub
folders) or is that a system call?

Many thanks,
-T

My final code:


sub RunCmd( Str $CommandStr, Bool $EchoOff = False )  {
    my Str $BatFile = $PathIAm ~ ".bat";
    my Str $RtnStr;
    my Str $CmdStr = "";

    if $EchoOff  { $CmdStr = Q[@echo off] ~ "\n"; }
    $CmdStr = $CmdStr ~ $CommandStr ~ "\n";
    # print "$CmdStr";

    spurt( $BatFile, $CmdStr );
    $RtnStr = qqx { $BatFile };
    # print "$RtnStr\n";
    return $RtnStr;
}


sub DirectoryExists( Str $DirPath     --> Bool ) { return "$DirPath".IO.d.Bool; }

sub FolderSize( Str $FolderPath --> Int )  {
    # dir . /s /A:-D /d /a  | raku -e "say lines[*-2].words[2]"
    # 3,275,848,795
    # > my Str $x="123,456,789"; $x~~s:g/","//; say $x; $y=$x.Int; say $y
    # 123456789
    # 123456789

    my $SubName = &?ROUTINE.name;
    my Str $RtnStr = "";
    my Str $ErrMsg = "";
    my Int $Size   = 0;

    if not DirectoryExists( $FolderPath )  {
       $ErrMsg = "$IAm\:$SubName error: FolderPath <$FolderPath> doesn not exist\n\n";
        MessageBox $IAm, $ErrMsg, MB_ICONERROR, MB_OK;
        print $ErrMsg;
        return 0;
    }



I wish Raku would let me declare more than
one item in the sub declaration.  This would
tickle me:

     sub FolderSize( Str $FolderPath --> Int, Str)  {

Maybe sometime in the future.

I tweaked the folder size a little bit to
capture the formatted string as well. It
already has the commas inserted.:


sub FolderSize( Str $FolderPath )  {
   # returns
   #    1) size as an integer         (123456789)
   #    2) size as a formatted string (123,456,789)

   my $SubName     = &?ROUTINE.name;
   my Str $RtnStr  = "";
   my Str $ErrMsg  = "";
   my Int $Size    = 0;
   my Str $SizeStr = "";

   if not DirectoryExists( $FolderPath )  {
$ErrMsg = "$IAm\:$SubName error: FolderPath <$FolderPath> doesn not exist\n\n";
       MessageBox $IAm, $ErrMsg, MB_ICONERROR, MB_OK;
       print $ErrMsg;
       return 0, "";
   }

   $RtnStr  = RunCmd Q[dir . /s /A:-D /d /a];
   $RtnStr  = $RtnStr.lines[*-2].words[2];
   $SizeStr = $RtnStr;
   $RtnStr ~~ s:global/ "," //;
   $Size = $RtnStr.Int;
   return $Size, $SizeStr;
}



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to