How to list all folder with size via batch file How to list all folder with size via batch file windows windows

How to list all folder with size via batch file


For each folder in the list, use dir command to retrieve the size of the files under the folder

@echo off    setlocal disabledelayedexpansion    set "folder=%~1"    if not defined folder set "folder=%cd%"    for /d %%a in ("%folder%\*") do (        set "size=0"        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"        setlocal enabledelayedexpansion        echo(%%~nxa # !size!        endlocal    )    endlocal

It iterates over the indicated folder (passed as parameter to the batch file, or current directory if there is no paramter).

For each folder inside it (for /d) a recursive dir command is executed inside the inner for command, and from its output, the summary line at the end (extracted by findstr) is parsed (the tokens in for command) and the total size of all the files under this subfolder is retrieved. Then the name (and extension if it has) of the folder and the size of the elements under it is echoed to console.

If a file needs to be created, redirect the output of the batch to a file

getSizes.cmd "c:\temp" > C:\folderList.txt


Using MC ND's excellent code, I've added conversion to Kb, Mb, Gb, etc. Just in case you'd rather have it in those formats.

@echo offsetlocal disabledelayedexpansionset "folder=%~1"  if not defined folder set "folder=%cd%"    for /d %%a in ("%folder%\*") do (        set "size=0"        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"        setlocal enabledelayedexpansion        call :GetUnit !size! unit        call :ConvertBytes !size! !unit! newsize        echo(%%~nxa - !newsize! !unit!        endlocal    )endlocalexit /b:ConvertBytes bytes unit retsetlocalif "%~2" EQU "KB" set val=/1024if "%~2" EQU "MB" set val=/1024/1024if "%~2" EQU "GB" set val=/1024/1024/1024if "%~2" EQU "TB" set val=/1024/1024/1024/1024> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)for /f "delims=" %%a in (   'cscript //nologo %temp%\tmp.vbs' ) do endlocal & set %~3=%%adel %temp%\tmp.vbsexit /b:GetUnit bytes returnset byt=00000000000%1Xset TB=000000000001099511627776Xif %1 LEQ 1024 set "unit=Bytes"if %1 GTR 1024   set "unit=KB"if %1 GTR 1048576  set "unit=MB"if %1 GTR 1073741824  set "unit=GB"if %byt:~-14% GTR %TB:~-14% set "unit=TB"endlocal & set %~2=%unit%exit /b


I took @Matt Williamsons code and made it export each line to a .csv file in the run directory, folderSizes.csv. It provides the full Byte size as a column so that you can easily sort in excel (or whatever).

@echo offecho Getting folder sizes for you...storing to folderSizes.csvsetlocal disabledelayedexpansionif EXIST folderSizes.csv del folderSizes.csvecho Folder,Bytes Size,Short Size > folderSizes.csvset "folder=%~1"  if not defined folder set "folder=%cd%"    for /d %%a in ("%folder%\*") do (        set "size=0"        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"        setlocal enabledelayedexpansion        call :GetUnit !size! unit        call :ConvertBytes !size! !unit! newsize        echo(%%~nxa,!size!,!newsize!!unit! >> folderSizes.csv        endlocal     )endlocalexit /b:ConvertBytes bytes unit retsetlocalif "%~2" EQU "KB" set val=/1024if "%~2" EQU "MB" set val=/1024/1024if "%~2" EQU "GB" set val=/1024/1024/1024if "%~2" EQU "TB" set val=/1024/1024/1024/1024> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)for /f "delims=" %%a in (   'cscript //nologo %temp%\tmp.vbs' ) do endlocal & set %~3=%%adel %temp%\tmp.vbsexit /b:GetUnit bytes returnset byt=00000000000%1Xset TB=000000000001099511627776Xif %1 LEQ 1024 set "unit=Bytes"if %1 GTR 1024   set "unit=KB"if %1 GTR 1048576  set "unit=MB"if %1 GTR 1073741824  set "unit=GB"if %byt:~-14% GTR %TB:~-14% set "unit=TB"endlocal & set %~2=%unit%exit /b