GetSize; A command-line utility for recursively getting directory size.

Easily retrieve the file size of a directory and all files and sub-directories using Powershell

This code shim explains how to create a quick little Batch Script that will utilise Powershell to get the total file size of a directory and all its child files and sub-directories.

This code depends upon: Windows (Vista+), Powershell

Preparation

  1. Create a folder on your computer to place utilities into. For the purpose of this guide I will use the path: “C:\_DevBin”
  2. Ensure you have Powershell correctly installed and you have the correct execution permissions set. You can find out the latter when attempting to execute the finished file on a restricted session. If you need to set any additional permissions you will need to do some further reading on your own.

GetSize code

@echo off
powershell -noprofile -command “switch((ls -r|measure -s Length).Sum) { {$_ -gt 1GB} { ‘{0:0.0} GiB’ -f ($_/1GB); break; }; {$_ -gt 1MB} { ‘{0:0.0} MiB’ -f ($_/1MB); break; }; {$_ -gt 1KB} { ‘{0:0.0} KiB’ -f ($_/1KB); break; } default { \”$_ bytes\”; } }”

Open a new notepad window, paste in the sample code and save the document as C:\_DevBin\GetSize.bat

Please note: All the above code should be on a single line – you may see this code wrap due to your screen/browser width. Please ensure that your resulting batch file contains all code on one line only – otherwise errors may occur.

Make the tool accessible from anywhere

Once you have a local folder with the GetSize file in, open your system’s Environment Variables dialog. This can vary from different versions of Windows, so you may need to check online for the exact method.

When you have the Environment Variables dialog open, edit the PATH System Environment Variable, and add to the start:

C:\_DevBin;

This will make all binaries and executables in the _DevBin folder available from any command prompt.

Test out your utility!

Open a new command prompt window and navigate to a folder that contains sub-folders. Type “GetSize” (without quotations) and hit enter – the utility will then calculate the total size of the directory and report it back on completion!

GetSize-demo

Download the Utility

Here’s one I made earlier – a download containing the code and a quick readme.

Download

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.