Keep an eye on the free space

Discuss and share scripts and script files...
Post Reply
LittleBiG
Posts: 1846
Joined: 08 Apr 2011 12:57
Location: Win10x64

Keep an eye on the free space

Post by LittleBiG »

Sometimes the free space on the system drive gradually runs out and you don't notice it in time. To avoid this I wrote a script to watch the space on drive C constantly, which now, in the new era of Custom Event Actions, can be very useful.

You have to choose a custom toolbar button, and you will see the free space (in gigabytes) as a label of it. If the free space on the drive is bigger than 999 GB, you will see the 999 in a green circle. It is possible for you to set a threshold. If the free space goes below this, a red circle will appear behind the number as an advance warning, which can be noticed quite easily and actually this is the main purpose of the script. By copying it into one of the "changing locations" of the CEA, the free space info on the button is refreshed on every folder change.

You have to set up your preferences in the first section of the code: the index of the button you use, the drive label you want to watch (empty means the current drive you are browsing on), and your desired threshold.

Code: Select all

//watch free space on a drive

  // --- settings
  $buttonindex=18; // the index of the custom toolbar button 
  $drivetowatch = "c:"; // empty is current
  $threshold = 5; // in GB
  // --- end of settings

  $freespace = get("freespace",$drivetowatch,1)\(1024^3); //in GB
  if ($freespace > 999) {
    $labeltext = 999; //do not exceed the 3 characters in label
  } 
  else {
    $labeltext = $freespace;
  }
  $labeltext = $labeltext . strrepeat(" ", 3-strlen($labeltext)); //keep it in the middle with spaces at the end
  if ($freespace < $threshold) {
    $labeltext = $labeltext . "*#FF0000"; // red circle if we are below the threshold
  }
  elseif ($freespace > 999) {
    $labeltext = $labeltext . "*#00FF00"; // if the free space is too big and above the threshold, draw a green circle
  }
  if ($drivetowatch == "") {
    $drivetowatch = "the current drive"; // if no drive is set, write "current" into the tooltip
  }
  else {
    $drivetowatch = "drive " . replace(recase($drivetowatch,"upper"),":","");
  }
  ctbname("Free space on $drivetowatch (in gigabytes)<crlf>Actual threshold is $threshold GB",$buttonindex);
  ctbicon("label: " . $labeltext,$buttonindex);

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: Keep an eye on the free space

Post by klownboy »

Hey LittleBiG, that's really slick and it works like a charm. I wonder if it would be worthwhile - possibly on right click of a CTB - to have a drop down for all the users hard drives, you'd select one, and the tooltip would update the info for that drive. In any case nice work :tup:

Along those lines. You've been around here a long time. Do you remember this serendipity script, DiskManager. viewtopic.php?p=85869#p85869 What a great script and it still works fine. I think DiskManager_v4.2.1.xys is the latest published. If you have syntax checking on though you'll get some errors but they are easily correctable.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

LittleBiG
Posts: 1846
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Keep an eye on the free space

Post by LittleBiG »

klownboy wrote: 07 Dec 2020 18:25 Hey LittleBiG, that's really slick and it works like a charm. I wonder if it would be worthwhile - possibly on right click of a CTB - to have a drop down for all the users hard drives, you'd select one, and the tooltip would update the info for that drive.
Actually I have a very simple script on the click event of that button to show the free spaces on all drives, but clicking on them does nothing.

Code: Select all

//free space on menu
  $list = "";
  foreach($drive, get("drives"),,, "") {
    if (exists($drive) != 0) {
      $list = $list . $drive . " - " . formatbytes(get("FreeSpace",$drive,1),"MB") . "<crlf>";
    }
  }
  popupmenu("FREE SPACE ON EXISTING DRIVES<crlf>-<crlf>" . formatlist($list,"e"),,,,,,"<crlf>");
klownboy wrote: 07 Dec 2020 18:25 In any case nice work :tup:
Thanks!
klownboy wrote: 07 Dec 2020 18:25 Along those lines. You've been around here a long time. Do you remember this serendipity script, DiskManager. viewtopic.php?p=85869#p85869 What a great script and it still works fine. I think DiskManager_v4.2.1.xys is the latest published. If you have syntax checking on though you'll get some errors but they are easily correctable.
Indeed I remember! An incredibly great one.

LittleBiG
Posts: 1846
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: Keep an eye on the free space

Post by LittleBiG »

Your idea, klownboy, kept me thinking that it can be useful for me, so as I had a couple of minutes, I added a new part to the button script. Clicking on a menu item will set the chosen drive to watch. If I choose the header, it will mean the current drive.

Code: Select all

//free space on menu
  $list = "";
  foreach($drive, get("drives"),,, "") {
    if (exists($drive) != 0) {
      $list = $list . $drive . " - " . formatbytes(get("FreeSpace",$drive,1),"MB") . "<crlf>";
    }
  }
  $answer = "";
  $answer = popupmenu("FREE SPACE ON EXISTING DRIVES<crlf>-<crlf>" . formatlist($list,"e"),,,,,,"<crlf>");
  if ($answer != "") {
    perm $p_drivetowatch; 
    if (substr($answer,0,2) == "FR") {
      $p_drivetowatch = "";
    }
    else {
      $p_drivetowatch = substr($answer,0,2); 
    }
    goto <xydata>; //to invoke the refresh immediately
    #527; //go back
  }
Also, I changed the "on location change" script a bit to accept the permanent variable. If it is not set, the default drive (in the settings section) will be watched. Practically I modified only 1 row and added 1 new:

Code: Select all

//watch free space on a drive

  // settings
  $buttonindex=18; // the index of the custom toolbar button 
  $threshold = 10; // in GB  
  $default_drivetowatch = "c:"; // empty is current
  // ---
 
  $drivetowatch = isset($p_drivetowatch)?$p_drivetowatch:$default_drivetowatch;

  $freespace = get("freespace",$drivetowatch,1)\(1024^3); //in GB
  if ($freespace > 999) {
    $labeltext = 999; //do not exceed the 3 characters in label
  }
  else {
    $labeltext = $freespace;
  }
  $labeltext = $labeltext . strrepeat(" ", 3-strlen($labeltext)); //keep it in the middle with spaces at the end
  if ($freespace < $threshold) {
    $labeltext = $labeltext . "*#FF0000"; // red circle if we are below the threshold
  }
  elseif ($freespace > 999) {
    $labeltext = $labeltext . "*#00FF00"; // if the freespace is too big but over the threshold, draw a green circle
  }
  if ($drivetowatch == "") {
    $drivetowatch = "the current drive"; // if no drive is set, write "current" into the tooltip
  }
  else {
    $drivetowatch = "drive " . replace(recase($drivetowatch,"upper"),":","");
  }
  ctbname("Free space on $drivetowatch (in gigabytes)<crlf>Actual threshold is $threshold GB",$buttonindex);
  ctbicon("label: " . $labeltext,$buttonindex);
I don't have time to polish the script further and I think it fully meets my expectations now. Also, I think it is easy enough to understand for users who wants to learn coding in XY.

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: Keep an eye on the free space

Post by klownboy »

Hi LittleBig, I had some time this past week so I embellished your nice script a little. I hope you don't mind. You did seem open to users playing with the code. The main reason I wanted to update it though was to take advantage of Don's new tweak("SkipBrowseEvents", "1|0"). With that tweak we can disable CEA for changing locations so therefore we can also turn on or off drive free space watch as desired (i.e., you may want to turn it on for a time and then disabled it when you've seen enough).

I've eliminated the need for a permanent variable as well by saving the watched drive in an onboard [INI] section.

Both the normal left click menu as well as the code for CEA changing locations is contained within the one script.

The CTB button index number will be obtained within the script when you run it. Please click on the CTB button first prior to changing locations such that the number is set first. Enter load "<xyscripts>\DiskSpaceWatch.xys"; for the left click action.

Hopefully the script is a bit more accessable to both new and old users to try it out.

- As LittleBig stated up front establish CTB to run the script.
- I've set up the script such that it will make the necessary changes to "CEA_AfterBrowse" and "CEA_AfterBrowse_Script". It will restart XYplorer when done. This will be required only once. If you'd rather, you can enter the info manually in CEA » Changing Locations » After Browsing a folder: Action: "Run script" and Script: load "<xyscripts>\DiskSpaceWatch.xys";
- A normal left click of the CTB will bring up the Drive Free Space Menu.
- Clicking on "Current drive" will watch whatever your current drive is and the CTB tooltip will change to indicate the free space on that drive. So any drive change will cause the CTB label & tooltip to change (e.g. which could be by actually selecting a diferent drive in the tree or via tab change. If you select an actual drive (letter), then only that selected drive is being watched for free space.
- You can disable drive free space watch at any time by clicking the main menu heading which indicates whether it's active or not. Right clicking the CTB will also disable drive free space watch if you had the script in the CTB right click action as well as left. Though you could reference a different script for right click.
- The tweak("SkipBrowseEvents") will remain in effect for subsequent sessions.
- The drive or current drive is flagged with a " •" The flag simply indicates the last drive selected whether drive watch is active or inactive.
- The menu title bar will indicate whether the drive free space is "active" or "inactive". Click on that title to change from "active" to "inactive". When you make drive watching inactive, the menu is not returned. The menu is re-displayed when you activate. Important: You can also activate drive watch simply by making a drive or "current drive" selection in the menu.

Again, many thanks to LittleBig for a very nice script.
DriveFreeSpaceWatch.jpg
DriveFreeSpaceWatch.jpg (17.01 KiB) Viewed 962 times
DiskSpaceWatch.xys
(9.05 KiB) Downloaded 75 times
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

Post Reply