Display Sum of Files and Folders' Sizes on both Panes.

Discuss and share scripts and script files...
Post Reply
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Display Sum of Files and Folders' Sizes on both Panes.

Post by SkyFrontier »

This will calculate and display file sizes' sum on both panes, two modes: one will sum up all files (no folders!) on both panes and show the results of each, the other will calculate selected items' (files and folders if any) sizes and display the results for comparison.
-requires XYplorer v +9.70.004;
-has borrowed code that checks for existing dual panes (stealing is an art, boys and girls! :wink: );
-display a great way to execute two scripts out of a description box (thanks for the tips, Stefan!);
-fixes a minor problem regarding residual file sizes displayed for folders;
-tells which value belongs to which pane;
-displays two methods for both dealing with eval and paragraphs on confirm boxes.

EDIT: Compatibility check.

>ToDo: rephrase wording (I feel there's something wrong with that as it is...). :P
For the sake of pleasant results' displaying, I'd really like to have a way/parameter to show such data with leading zeros auto-filled by XY based on the longer number out of outputs...
Current:

Code: Select all

::Echo("File sizes on pane 1 are: 234.345 bytes,<crlf>  while in the pane 2 are 1.325 bytes.<crlf><crlf>Included size of selected folders too,<crlf>if any.<crlf>-Have a nice day!");
Desired:

Code: Select all

::Echo("File sizes on pane 1 are: 234.345 bytes,<crlf>  while in the pane 2 are 001.325 bytes.<crlf><crlf>Included size of selected folders too,<crlf>if any.<crlf>-Have a nice day!");
-see the difference...? (run those codes on address bar and you'll know what I'm talking about)

Code: Select all

//works on XYplorer v +9.70.004
   end (<xyver> < "9.70.0004"), "Sorry - this script requires XYplorer version 9.70.0004 or higher.<crlf>Click 'Ok' to exit.";
   $answer = confirm("Sum up sizes of all files only or<br>calculate selected files and folders only?<br>-will work on both panes!<br><br>OK for All Files' sum,<br>Cancel for Selected Files and Folders.")?"_ZERO":"_ONE";
    sub $answer;

"_ZERO"
    if (get("#800") == "0") {
        #800;
    }
   $pa = <get pane>;
   $a = Eval(Report("+{Dir 0|{Size B}|0}", 0, "0"));
   focus pi;
   $pi = <get pane>;
   $b = Eval(Report("+{Dir 0|{Size B}|0}", 0, "0"));
   focus pi;
   Echo("File sizes on pane $pa are: $a bytes,<crlf> while in the pane $pi are: $b bytes.<crlf>-Have a nice day!");

"_ONE"
    if (get("#800") == "0") {
        #800;
    }
   $pa = <get pane>;
   #487;
   $c= report ("{Size B}+", 1); $t= substr ($c, 0,-1); replace $t, $t, "-","0"; $t= eval ($t);
   #485;
   focus pi;
   $pi = <get pane>;
   #487;
   $c2= report ("{Size B}+", 1); $t2= substr ($c2, 0,-1); replace $t2, $t2, "-","0"; $t2= eval ($t2);
   #485;
   focus pi;
//   setting "ShowFolderSizeInList", 0;
   Echo("File sizes on pane $pa are: $t bytes,<crlf>  while in the pane $pi are $t2 bytes.<crlf><crlf>Included size of selected folders too,<crlf>if any.<crlf>-Have a nice day!");
Last edited by SkyFrontier on 12 Nov 2010 11:36, edited 1 time in total.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

jjk
Posts: 202
Joined: 10 Oct 2007 20:11
Location: Paris

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by jjk »

Here, on a French configured OS, the thousand separator is chr(160).
So I have to replace in your last script :
Size B by Size RAW
and
$t= eval ($t); by replace $t, $t, chr(160),""; $t= eval ($t);
and
$t2= eval ($t2); by replace $t2, $t2, chr(160),""; $t2= eval ($t2);

If not, only digits before the thousand separator are sum'd

But, even after those replacements, I have another issue : the results contain no thousand separator. And because results are in bytes (and not in Mb or Gb or Tb !), they are very difficult to be read.

Have you any (simple) solution
1) to add a thousand separator in report of large numbers
2) to know the thousand separator in any localisation, or to sum numbers which contain a thousand separator, by eval function or anyone else ?

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by TheQwerty »

SkyFrontier, now you see why my original script used {Size RAW}, :P
HINT: Take a look at Format() to give your final sum digit grouping again.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by SkyFrontier »

Hi, jjk!
I will try to locate/develop a solution for such cases (I was afraid of that - on another script [Select Items Modified Today] I managed to adapt report/formatdate to reflect localization to meet foreign users' standards, but that was a really simple solution.
Besides the lack of deeper scripting abilities, I won't promise I'll try that hard because I can't even emulate your regional settings - but I may have anywhere an old image of a Win2K/English so I may be able to try at least, if something comes up to mind first.
-any other scripter out there, you're welcome to help on this!
---
@TheQwerty - yeah, I see... >sigh< (I was wondering on the difference and took that approach for learning purposes, but it's a bit hard to guess all of the time without any guidance the impact of such differences on real world scenarios one can't even emulate - once again, thank you, TQ!)
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

jjk
Posts: 202
Joined: 10 Oct 2007 20:11
Location: Paris

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by jjk »

Thank you both

I have used format() and it is nice. Now your script works.

But can you say why you have replaced "-" by "0" in $t

Code: Select all

replace $t, $t, "-","0"
Now, I ask to Don whether there would be some scripting variable to indicate the localized separators.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by SkyFrontier »

jjk wrote: I have used format() and it is nice. Now your script works.
...so what about posting your final, working version for european users out there? :wink:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

jjk
Posts: 202
Joined: 10 Oct 2007 20:11
Location: Paris

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by jjk »

So, here is my script. But there is a mix of English and French texts.
And chr(160) is not the thousand separator for all european users, for French and some other ones only.

Code: Select all

//works on XYplorer v +9.70.004
   $answer = confirm("Compter la taille de tous les fichiers ou<br>des fichiers et dossiers sélectionnés ?<br>(Calcule les deux volets)<br><br>OK pour tous fichiers,<br>Annuler pour sélection")?"_ZERO":"_ONE";
    sub $answer;

"_ZERO"
    if (get("#800") == "0") { #800; }
   $pa = <get pane>;
   $a = format(Eval(Report("+{Dir 0|{Size RAW}|0}", 0, "0")),"# ### ### ##0");
   focus pi;
   $pi = <get pane>;
   $b = format(Eval(Report("+{Dir 0|{Size RAW}|0}", 0, "0")), "# ### ### ##0");
   focus pi;
   Echo("File sizes on pane $pa are: $a bytes,<crlf> while in the pane $pi are: $b bytes.<crlf>-Have a nice day!");

"_ONE"
    if (get("#800") == "0") { #800; }
   $pa = <get pane>;
   #487;
   $c= report ("{Size B}+", 1); $t= substr ($c, 0,-1); replace $t, $t, "-","0"; replace $t, $t, chr(160),""; $t= format(eval ($t),"# ### ### ##0");
   #485;
   focus pi;
   $pi = <get pane>;
   #487;
   $c2= report ("{Size B}+", 1); $t2= substr ($c2, 0,-1); replace $t2, $t2, "-","0"; replace $t2, $t2, chr(160),""; $t2= format(eval ($t2),"# ### ### ##0");
   #485;
   focus pi;
//   setting "ShowFolderSizeInList", 0;
   Echo("File sizes on pane $pa are: $t bytes,<crlf>  while in the pane $pi are $t2 bytes.<crlf><crlf>Included size of selected folders too,<crlf>if any.<crlf>-Have a nice day!");
And what was the reason of replace $t, $t, "-","0" ?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by SkyFrontier »

jjk wrote: And what was the reason of replace $t, $t, "-","0" ?
In case of any setting to display folders containing "-" to indicate empty folders, this won't impact in the calculations. Haven't tested but the "-" value could be replaced by any other value ("zero", "nada", "phoenix" :P ) user wants with zero impact on final results.
---
References:

Code: Select all

v9.11.0012 - 2010-05-27 13:09
    + Tweak to customize what's displayed for empty folders in the Size 
      column when showing folder sizes.
        FolderEmpty="[Empty]"
      Factory default is "[Empty]".

v9.11.0004 - 2010-05-22 13:35
    * List: When showing folder sizes, empty folders now display the 
      label "[Empty]" in the Size column. Before they displayed "-" 
      which lead to confusion among various users.
EDIT: Added the second reference.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by Stefan »

jjk wrote:Now, I ask to Don whether there would be some scripting variable to indicate the localized separators.
http://www.windowsitpro.com/article/reg ... stry-.aspx
sThousand Thousands separator. The default for U.S. English is , (comma).

But more general use would be...
...can XY scripting read the registry?
$separator = regread("HKEY_CURRENT_USER\Control Panel\International\sThousand");


Or do we need to make workarounds with CMD, VBS or AHK and an temporary file for this?


 EDIT

regread example for reference

Code: Select all

$key = quote("HKEY_CURRENT_USER\Control Panel\International");
   $value = "sCountry";   //"sThousand";
   
   $tmp = "c:\temp\xyregquerytest.txt";
   
   
   run "cmd /c reg query $key /v $value | find " . quote($value) . " > $tmp";
   $readValue = readfile($tmp); 
   
    
   //clean-up
   $readValue = replace($readValue, $value);
   $readValue = replace($readValue, "REG_SZ");
   $readValue = replace($readValue, "REG_DWORD");
   $readValue = regexreplace($readValue, "<crlf>");
   $readValue = regexreplace($readValue, "\s*(.+)\s*", "$1");
   
   msg "$readValue";
Last edited by Stefan on 10 Nov 2010 22:04, edited 1 time in total.

admin
Site Admin
Posts: 65334
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by admin »

Tomorrow you get this:

Code: Select all

    + Scripting got a new function.
      Name:   formatbytes
      Action: Formats a number as bytes, resp. converts byte formats.
      Syntax: formatbytes(bytes, [format=FLEX])
        bytes:    The number to format.
                  Can be a raw number or a string like "2 KB".
        format:   The desired formatting. One of the following:
            FLEX = [Default] Flexible (best unit is auto-selected).
            KB, MG, GB, TB, or PB = usual bytes unit.
            BB = Bytes (with unit)
            B = Bytes (no unit)
            RAW = Raw number (no unit, no thousand separators).
        return: Formatted number.
      Remarks:
      - The returned numbers are rounded to 2 digits and have thousand 
        separators (unless "R" is stated as format).
      - Note that the function is locale-specific! Decimal and thousand 
        separators may be different from locale to locale.
      Examples for US locale:
        ::text formatbytes("1.34 MB", "KB");     //1,372.16 KB
        ::text formatbytes("1,372.16 KB", "MB"); //1.34 MB
      Examples for German locale:
        ::text formatbytes("1,34 MB", "KB");     //1.372,16 KB
        ::text formatbytes("1.372,16 KB", "MB"); //1,34 MB      
      More examples for German locale:  
        ::text formatbytes("2 kb", "b"); //2.048
        ::text formatbytes("2 kb", "bb"); //2.048 bytes
        ::text formatbytes("2048", "FLEX");  //2,00 KB
        ::text formatbytes("2048");  //2,00 KB
        ::text formatbytes(2048);    //2,00 KB
        ::text formatbytes(2048 * 1024) //2,00 MB
        ::text formatbytes("1,5 MB", "RAW") //1572864

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by SkyFrontier »

Code: Select all

    + Scripting got a new function.
      Name:   formatbytes
...that was unexpected! Thank you! :D
EDIT: Next project: XY Unit Converter! hehehehe... (assuming I have the know-how... :wink: )
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Display Sum of Files and Folders' Sizes on both Panes.

Post by SkyFrontier »

Improved version. This is supposed to solve localization problems with the first script, based on recent changes*.
>If you know a way to integrate the longish "msg" box with "input" functionality, please tell me. (three screens to get what you want = baaaaad usability, man!)

Code: Select all

//Display Sum of Files and Folders' Sizes on both Panes
//works on XYplorer v +9.70.0007
   end (<xyver> < "9.70.0007"), "Sorry - this script requires XYplorer version 9.70.0007 or higher.<crlf>Click 'Ok' to exit.";
   $answer = confirm("Sum up sizes of all files only or<br>calculate selected files and folders only?<br>-will work on both panes!<br><br>OK for All Files' sum,<br>Cancel for Selected Files and Folders.")?"_ZERO":"_ONE";
    sub $answer;

"_ZERO"
    if (get("#800") == "0") {
        #800;
    }
   $pa = <get pane>;
   $a = Eval(Report("+{Dir 0|{Size B}|0}", 0, "0"));
   focus pi;
   $pi = <get pane>;
   $b = Eval(Report("+{Dir 0|{Size B}|0}", 0, "0"));
   focus pi;
   Echo("File sizes on pane $pa are: $a bytes,<crlf> while in the pane $pi are: $b bytes.<crlf>-Have a nice day!");

"_ONE"
   msg "This will sum up all selected items and display their values<crlf>in the unit you indicate in the next screen. Allowed:<crlf><crlf>FLEX = [Default] Flexible (best unit is auto-selected).<crlf>FLEXR = FLEX, but rounded up.<crlf>KB, MB, GB, TB, or PB = usual bytes unit.<crlf>KBR = KB, but rounded up.<crlf>BB = Bytes (with unit)<crlf>B = Bytes (no unit)<crlf>RAW = Raw number (no unit, no thousand separators).", 1, ;
   input $v, "Please enter the unit you want (FLEX/FLEXR/KB, MB, GB, TB, or PB/KBR/BB/B/RAW):", "MB";
    if (get("#800") == "0") {
        #800;
    }
   $pa = <get pane>;
   #487;
   $c= report ("{Size RAW}+", 1); $t= $c; replace $t, $t, "-","0"; $t= eval ($t);
   $res = formatbytes($t, "$v", -1);
   #485;
   focus pi;
   $pi = <get pane>;
   #487;
   $c2= report ("{Size RAW}+", 1); $t2= $c2; replace $t2, $t2, "-","0"; $t2= eval ($t2);
   $res2 = formatbytes($t2, "$v", -1);
   #485;
   focus pi;
//   setting "ShowFolderSizeInList", 0;
   Echo("File sizes on pane $pa are: $res,<crlf>  while in the pane $pi are $res2.<crlf><crlf>Included size of selected folders too,<crlf>if any.<crlf>-Have a nice day!");
---
*Reference:

Code: Select all

v9.70.0007 - 2010-11-11 13:29
    + Scripting got a new function.
      Name:   formatbytes
      Action: Formats a number as bytes, resp. converts byte formats.
      Syntax: formatbytes(bytes, [format=FLEX], [decimals=-1])
        bytes:    The number to format.
                  Can be a raw number or a string like "2 KB".
        format:   The desired formatting. (...)
        decimals: The number of decimal points.
                  Ignored if the unit is B, BB, KBR, or FLEXR.
            -1 = System default.
        return: Formatted number.
EDIT: Fixed misspelling.
EDIT 2: Compatibility check added.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply