How to list all folder with size via batch file

心已入冬 提交于 2019-11-27 05:17:56

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 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

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.

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.

try for Windows:

dir /-c /a /w /s|sed -nr "/:$/q;/:/h;/^\s+[0-9]/{s/.*[^0-9]([0-9]+.*)/\1/;H;g;s/\n/ /p}"
Leland

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!