Thread: Filesizes

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Youngstown, OH, USA
    Posts
    5
    Rep Power
    0

    Filesizes


    Hello Dev-Shed Forums:

    This is my second week using and studying Perl.

    I am "most of the way" there on a script that I wrote to give me filesizes for my backup /mount_dir

    It is only my second script.

    perl Code:
    #!/usr/bin/perl -w
     
    ##########
    #
    # Project     : c9backups disk usage Report
    # Started     : 11/02/2012 03:17:05 PM
    # Author      : JJ of cirrhus9.com
    # Description : Read /c9backups and make a Report.
    #
    ##########
     
    @files = </c9backups/c9*/*.gz>;
     foreach $file (@files) {
            $filesize = -s $file;
        if ($filesize > 1024) {
            $filesize=sprintf("%0.2d", ($filesize/1024)); #KB
            print "$file = $filesize kb\n"; } else {
            print "$file = $filesize mb\n";
        }
    }


    but all I manage to get correctly is kb:
    Code:
    /c9backups/c9bacula/c9bacula-ScriptedDump_11-01-2012.1351828501.tar.gz = 118 kb
    /c9backups/c9bacula/c9bacula-ScriptedDump_11-02-2012.1351914901.tar.gz = 118 kb
    /c9backups/c9bacula/c9bacula-ScriptedDump_11-03-2012.1352001301.tar.gz = 118 kb
    /c9backups/c9bacula/c9bacula-ScriptedDump_11-04-2012.1352091301.tar.gz = 118 kb
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-01-2012.1351839301.tar.gz = 22651 kb
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-02-2012.1351925701.tar.gz = 22651 kb
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-03-2012.1352012101.tar.gz = 22651 kb
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-04-2012.1352102101.tar.gz = 22651 kb
    /c9backups/c9zabbix/c9zabbix-ScriptedDump_11-01-2012.1351828501.tar.gz = 349132 kb
    /c9backups/c9zabbix/c9zabbix-ScriptedDump_11-02-2012.1351914901.tar.gz = 352352 kb
    /c9backups/c9zabbix/c9zabbix-ScriptedDump_11-03-2012.1352001301.tar.gz = 355203 kb
    /c9backups/c9zabbix/c9zabbix-ScriptedDump_11-04-2012.1352091301.tar.gz = 358551 kb
    /c9backups/c9wiki/* and
    /c9backups/c9zabbix/* coughing up kb instead of mb.

    I borrowed the code from http://forums.devshed.com/showpost.php?p=2264323&postcount=2

    I have played with various incarnations of
    "$filesize=sprintf("%0.2d", ($filesize/1024)); #MB" but I either got both kb and mb or the current output.

    I am not sure where my error is and I need help with this basic task.

    Can someone help me out?

    Thank you for your time.

    I read at http://perl-begin.org/tutorials/bad-elements/#foreach-lines that using foreach is bad form?

    but that's all my meager skills can do atm.

    Thanks!
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,890
    Rep Power
    6444
    perl Code:
    foreach $file (@files) {
        $filesize = -s $file;
    	$filesize=sprintf("%0.2d", ($filesize/1024))." kb"; #KB
        if ($filesize > 1024) {
          $filesize=sprintf("%0.2d", ($filesize/1024))." mb" # MB
        } 
    	print $file."\t\t\t".$filesize."\n";
    }
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    It seems to me that your code lacks a condition to check whether the file is more than 1024 kilobytes and also lacks the division by 1024 in such a case, so that when you write so many megabytes, you are actually printing the number of bytes.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Youngstown, OH, USA
    Posts
    5
    Rep Power
    0
    Thank you both.
    Axweildr's snippet gets me further.
    I now have this output using it:
    Argument "118 kb" isn't numeric in numeric gt (>) at jj9.pl line 15

    partial output # showing [km]b identifiers
    Code:
    /c9backups/c9bacula/c9bacula-ScriptedDump_11-04-2012.1352091301.tar.gz	118 kb
    Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-01-2012.1351839301.tar.gz	22 mb
    Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-02-2012.1351925701.tar.gz	22 mb
    Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-03-2012.1352012101.tar.gz	22 mb
    Argument "22651 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
    /c9backups/c9wiki/c9wiki-ScriptedDump_11-04-2012.1352102101.tar.gz	22 mb
    Argument "349132 kb" isn't numeric in numeric gt (>) at jj9.pl line 15.
    /c9backups/c9zabbix/c9zabbix-ScriptedDump_11-01-2012.1351828501.tar.gz	340 mb
    I could guess what I need to do all day long, but I prefer not to in this case.
    But my gut and screen both scream at me the kb in the output is what is causing the "error" and I am not sure how to proceed to correct that.


    Thank you both for your time,

    Edit0:
    I removed the -w on the shebang line and now it's "better".
    It may not be 'correct' but I still await a solution with better 'form'.

    Thanks!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    Don't remove the -w from the shebang, or, rather, do it, but add a "use warnings;" line. And correct the reason for the warnings.

    The point is that you should store the number of bytes, kB, etc. as a pure number, and, on the other side, without the string " kB") and add the " kB" thing when you print. But if you are trying to do a numeric comparison (< or >) on "22651 kb" and some other value), the program is telling quite rightly that it does not make much sense.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Youngstown, OH, USA
    Posts
    5
    Rep Power
    0
    Thanks Laurent_R:

    I gathered this also.
    Page 27 (6th Ed.) of the Llama book specifically covers this situation, but does specifically say how to deal with it. So I read on and look for a solution.

    My code.fu tells me that the instruction I need to work on is "$filesize = -s $file;" or manipulate that output just after|below that line, or...something along those lines.

    I'll get there!

    Thanks for all your help.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    This is how I'd write that loop.
    Code:
    foreach my $file ( @files ) {
        my $filesize = -s $file;
        my ($div, $byte) = $filesize < 1024 ? (1, 'B')
                                            : $filesize < 1024**2 ? (1024, 'KB')
                                            : $filesize < 1024**3 ? (1024**2, 'MB')
                                            : (1024**3, 'GB');
        printf("$file = %0.2d $byte\n", $filesize/$div);
    }

    Comments on this post

    • JJ_of_c9 agrees : excellent solution given.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Youngstown, OH, USA
    Posts
    5
    Rep Power
    0
    Thanks FishMonger!

    The code is "perfect" but what I need to know is if my logic was "on target" or not in my last reply/post?

    I have been a shell script/bash coding enthusiast for years, and I took on the discipline of Perl with the hopes that my experience in the former could apply to the latter (being Perl).

    I understand also if my "logic" doesn't register with anyone as I am an odd duck. Old(er) and rather particular in my methods. It is also known to me that there are many, many ways to skin a particular cat in Perl.

    I appreciate your time.

    Thank you,
    JJ
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    The logic used in your opening post is wrong. If you're going to use > to test for greater-than, then you need to start at the upper end of the scale. Your logic states that all filesizes less than 1024 (1KB) are MB, which clearly is wrong.

    Axweildr's logic is a little better but incomplete and creates a bug by turning the filesize into a string and then attempts to use that string in a numerical calculation/test.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Youngstown, OH, USA
    Posts
    5
    Rep Power
    0
    Thank you for your response.

    I appreciate your time.

IMN logo majestic logo threadwatch logo seochat tools logo