Problem getting folder sizes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 154
Joined: 12 Aug 2019 13:57

Problem getting folder sizes

#1 Post by PAB » 29 Aug 2025 14:08

Good evening.

I hope all are well.

I have the following code which works besides getting the folders total sizes.

Code: Select all

@echo off
set "gtSize=25"

PowerShell ^
     $ErrorActionPreference='SilentlyContinue'; ^
     $CMD=Get-ChildItem -Path D:\ -Directory -Depth 0 ^| Sort-Object -Property {$_.FullName}; ^
     $Size=[Math]::((Get-ChildItem -Path D:\ -Directory -Depth 0 -Recurse $_.FullName ^| Measure-Object Length -Sum).Sum); ^
     $Tot =($CMD ^| Measure-Object).Count.ToString('#,##0'); ^
     $List=($CMD ^| Format-Table -AutoSize ^
     @{L='Full Name' ;E={;if([string]::IsNullOrWhiteSpace($_.FullName))                                   {'-'} else {$_.FullName}}}, ^
     @{L='Total Size';E={;if($_.Size -lt 1073741824) {;[Math]::Round($_.Size / 1MB, 2, 00).ToString('#.00 MB')} else {;[Math]::Round($_.Size / 1GB, 2, 00).ToString('#.00 GB')}};A='Right'} ^| ^
Out-String -Width 1000).Trim("""`r`n"""); ^
     if ($List.Length) {Write-Host """`n `n___ Folders Greater Than [%gtSize% MB] Size is [$Tot] ___`n`n `n`n$List"""} else ^
                       {Write-Host """`n `n___ NO Folders Greater Than [%gtSize% MB] Size is Available ___"""; exit 1} >> %Temp%\A.txt
     for /f "delims=" %%i in (%Temp%\A.txt) do echo. %%i
     del %Temp%\A.txt >nul 2>&1

echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
I would like to keep the above format if possible please.

Any help will be greatly appreciated.

Post Reply