Get size of a directory in ‘MB’ using batch file

You can do it with a hybrid script [Batch/Vbscript] like this one :

@echo off
set Folder="C:\temp"
echo The size of %Folder% is 
Call :GetSize %Folder%
echo wscript.echo GetSize("%~1"^)
echo Function GetSize(MyFolder^)
echo    Set fso = CreateObject("Scripting.FileSystemObject"^)
echo    Set objFolder= fso.GetFolder(MyFolder^)  
echo    GetSize = FormatSize(objFolder.Size^)
echo End Function
echo '*******************************************************************
echo 'Function to format a number into typical size scales
echo Function FormatSize(iSize^)
echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo    For i = 0 to 4
echo        If iSize ^> 1024 Then
echo            iSize = iSize / 1024
echo        Else
echo            Exit For
echo        End If
echo    Next
echo    FormatSize = Round(iSize,2^) ^& " " ^& aLabel(i^)
echo End Function
echo '*******************************************************************
Cscript /NoLogo %tmp%\Size.vbs
Del %tmp%\Size.vbs
Exit /b

Edit : on 30/03/2016 @12:11

And this a nice trick

the Liviu’s hack for embedding vbscode in batch without temp files

I just discovered from npocmaka thanks to him

@echo off
Set Folder="c:\temp"
@cScript.EXE //noLogo "%~f0?.WSF" %Folder%  //job:info %~nx0%*
@exit /b 0
<job id="info">
<script language="VBScript">
wscript.echo GetSize(WScript.Arguments(0))
Function GetSize(MyFolder)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder= fso.GetFolder(MyFolder)  
    GetSize = FormatSize(objFolder.Size)
End Function
'Function to format a number into typical size scales
Function FormatSize(iSize)
   aLabel = Array("bytes", "KB", "MB", "GB", "TB")
   For i = 0 to 4
        If iSize > 1024 Then
            iSize = iSize / 1024
            Exit For
        End If
   FormatSize = Round(iSize,2) & " " & aLabel(i)
End Function

Leave a Comment