perl regex matching challenge finding size of folder and sub-folders on Windows

Viewed 79

I'm very new to programming but have written a script that uses use File::Find to calculate the size of a folder and sub-folders using the following line from an example:

find(sub { $total += -s if -f }, "$scanlocation.vir");

This works most of the time, however, there was one folder that was very deep and it displayed the error: Can't cd to ../../../../../../.. and stopped.

I've tried referencing the scan location by local drive letter, network assigned letter from another computer and even a UNC \server\share format. Referring to the local drive letter, displays the error and the other two methods do not error but also do not calculate the total.

I have a directory listing that ends like this:

 Directory of x:\ready\14235.20013336.vir\2020-01-13\HP27680588713\PUBLIC\archive\Rasphone

    03/07/2020  05:24 PM    <DIR>          .
    03/07/2020  05:24 PM    <DIR>          ..
    06/24/2013  05:22 PM                 2 rasphone.pbk
                   1 File(s)              2 bytes

     Directory of x:\ready\14235.20013336.vir\2020-01-13\HP27680588713\PUBLIC\archive\WirelessKeyView

    03/07/2020  05:24 PM    <DIR>          .
    03/07/2020  05:24 PM    <DIR>          ..
    01/13/2020  06:39 PM                 0 WirelessKeyView.txt
    01/13/2020  06:39 PM                 0 WirelessKeyViewExport.txt
                   2 File(s)              0 bytes

         Total Files **Listed:
                8693 File(s)  8,711,887,757 bytes**
                5852 Dir(s)  98,156,724,404,224 bytes free

And thought a regex could pull out the value. I've gotten close with /Listed:.*File\(s\).*?bytes/gs but I can only match down to the bolded part above.

My ultimate goal is to get the size on disk of a specified folder including sub-folders in bytes. I'm already creating the txt file with the direct output.

So, any suggestions or point me in the right direction would be appreciated.

Thank you.

1 Answers
my $data = do { local $/; <DATA> };

my ($total) = $data =~ /Total[ ]Files[ ]Listed: \s+ \d+ \s+ File\(s\) \s+ ([\d,]+) \s+ bytes/x
   or die("No match");

print("Total: $total\n");

__DATA__
03/07/2020  05:24 PM    <DIR>          .
03/07/2020  05:24 PM    <DIR>          ..
06/24/2013  05:22 PM                 2 rasphone.pbk
               1 File(s)              2 bytes

 Directory of x:\ready\14235.20013336.vir\2020-01-13\HP27680588713\PUBLIC\archive\WirelessKeyView

03/07/2020  05:24 PM    <DIR>          .
03/07/2020  05:24 PM    <DIR>          ..
01/13/2020  06:39 PM                 0 WirelessKeyView.txt
01/13/2020  06:39 PM                 0 WirelessKeyViewExport.txt
               2 File(s)              0 bytes

     Total Files Listed:
            8693 File(s)  8,711,887,757 bytes
            5852 Dir(s)  98,156,724,404,224 bytes free
Related