How to list all folder with size via batch file

72,329

Solution 1

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

Solution 2

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 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
        call :GetUnit !size! unit
        call :ConvertBytes !size! !unit! newsize
        echo(%%~nxa - !newsize! !unit!
        endlocal
    )

endlocal
exit /b

:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~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=%%a
del %temp%\tmp.vbs
exit /b


:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %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

Solution 3

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 off
echo Getting folder sizes for you...storing to folderSizes.csv
setlocal disabledelayedexpansion
if EXIST folderSizes.csv del folderSizes.csv
echo Folder,Bytes Size,Short Size > folderSizes.csv

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
        call :GetUnit !size! unit
        call :ConvertBytes !size! !unit! newsize
        echo(%%~nxa,!size!,!newsize!!unit! >> folderSizes.csv
        endlocal 
    )

endlocal
exit /b

:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~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=%%a
del %temp%\tmp.vbs
exit /b


:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %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

Solution 4

My JREN.BAT utility can be used to get a list of folders with sizes. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.

JREN does not conveniently convert to MB (or any other unit) - it simply lists the size in bytes. But it sure is convenient (and comparatively fast) to get the listing:

jren "$" "' # '+size()" /d /j /list /p "d:\temp" >"C:\folderList.txt"

You might consider putting the folder size first, space padded to a fixed width that exceeds the biggest folder, followed by the folder name. I find this format much easier to read, and it is still easy to parse:

jren "^" "size('               ')+'  '" /d /j /list /p "d:\temp" >"C:\folderList.txt"

The output would look something like this

           1852  SomeFolderName
        1616869  Another folder name
         137764  yetAnother

Since JREN is a batch file, you must use CALL JREN if you put the command within another batch script.

Solution 5

if you are using windows 7 or new use powershell and type command

ls- r >> log.txt

it will list all the files in current directory along with file size in bytes to log file.

Share:
72,329

Related videos on Youtube

goldenbutter
Author by

goldenbutter

Updated on July 09, 2022

Comments

  • goldenbutter
    goldenbutter almost 2 years

    I want a simple solution for list of folders and size of them in either txt or csv format.

    I use this code for folder list

    dir C:\Temp\*.* /b /a:d > C:\folderList.txt
    

    current output

    <<folderList.txt>>
    folder1
    folder2
    folder3
    

    desired output

    <<folderList.txt>>
    folder1 # 100 MB
    folder2 # 30 MB
    folder3 # 110 MB
    

    Simply it would generate the size of each folder.. How can I proceed?? any help

  • goldenbutter
    goldenbutter over 10 years
    How this will work? where is the output file? and where is the input folder path? please clarify this..
  • MC ND
    MC ND over 10 years
    @goldenbutter, Added aclarations and sample of usage.
  • goldenbutter
    goldenbutter over 10 years
    thank you @MC ND ... its working I have added a file name for apending all list at a place... Thanx
  • goldenbutter
    goldenbutter over 10 years
    just trying to customize the output.... instead BYETS the output will come as KB .... i tried "size=%%~b"/1024 or !size!/1024 ... but it is printing all.... :(
  • MC ND
    MC ND over 10 years
    @goldenbutter, set /a "size=%%~b/1024" BUT, in batch files, arithmetic is limited to 32bit signed integers. If you have a folder with more than 2GB data, then you will not be able to do it that way. If absolute precision is not needed, you can echo !size:~0,-3!, or you will need to add code to handle it.
  • goldenbutter
    goldenbutter over 10 years
    Its amazing....working perfectly.... thanks for formatting the UNIT.... @Matt Willamson
  • Drew
    Drew over 8 years
    This runs however it is only listing the folders from the top level and does not recursively go through all folders...suggestions?
  • Eli Borodach
    Eli Borodach over 6 years
    How will you process large directories in size, which have a higher than 32-bit integer? Something like certain GB and TB?
  • George Birbilis
    George Birbilis over 4 years
    this won't provide output of the style "folder1 # 100 MB", aka recursively iterate subfolders and sum file sizes of the files in them
  • George Birbilis
    George Birbilis over 4 years
    @Drew it does seem to recurse fine at the level where you place/run it, do you mean you want to see the intermediate results too (aka sizes of each subfolder and sub-subfolder etc.)?
  • goldenbutter
    goldenbutter over 3 years
    is it possible to get all files including all folders... in the same list ? @MC ND