How could I display the subdirectories of a folder from largest to smallest using the dir command?

I've tried using dir /O:-S command, and although it sorts files just fine, it doesn't seem to order the subdirectories.

Ideally, the command should be able to go down several levels; some of these sub-folders have their own folders. For example:

D:/
|-- Folder 1
    |-- Subfolder 1
    +-- Subfolder 2
        |--Another folder
+-- Folder 2

Suppose the total size of Folder 1 (including all files in its subfolders) is 10GB, and that of Folder 2 is 15GB, how would I output their order sorted by total content size?

I.e.

94932485 Folder 2
6453445  Folder 1

Thanks in advance!

有帮助吗?

解决方案 2

This seems to work for the changed requirements: alter c:\folder to the folder level you want to query.

@echo off
pushd "c:\folder"
for /f "delims=" %%a in (' dir /ad /b ') do call :size "%%~fa"
sort /r < "%temp%\dirsize.tmp"
del "%temp%\dirsize.tmp"
popd
pause
goto :eof

:size
for /f "tokens=3" %%b in ('dir /s "%~1" 2^>nul ^|find " File(s) "') do set "n=%%b"
set dirsize=%n%
REM set dirsize=%dirsize:,=%
set dirsize=                 %dirsize%
set dirsize=%dirsize:~-18%
>>"%temp%\dirsize.tmp" echo %dirsize% "%~1"

其他提示

If you are after a one-line solution, that supports upto 999 terabytes:

cmd /v /c "set zeropad=000,000,000,000,000,&for /f "delims=" %a in ('dir /ad /b') do @set bytes=!zeropad!000&(for /f "tokens=3" %b in ('dir /s "%a" 2^>NUL ^| find "File(s)"') do @set bytes=%b)& @for /f "tokens=1* delims=," %c in ('echo !bytes!') do @(set bytes=%c&@set bytes=000!bytes!&@set bytes=!bytes:~-3!& @set bytes=!zeropad!!bytes!&if "%d" NEQ "" set bytes=!bytes!,%d) & @echo !bytes:~-23! %a" | sort /R

And a bonus one-line solution if you want both files and directories

cmd /v /c "set zeropad=000,000,000,000,000,&for /f "tokens=4* delims= " %a in ('dir ^| find "/" ^| findstr /E /V /R "DIR^>[ ][ ]*\.\.$ DIR^>[ ][ ]*\.$"') do @set bytes=!zeropad!000&(if "%a" EQU "^<DIR^>" (for /f "tokens=3" %c in ('dir /s "%b" 2^>NUL ^| find "File(s)"') do @set bytes=%c)) & (if "%a" NEQ "^<DIR^>" (set bytes=%a)) & (for /f "tokens=1* delims=," %d in ('echo !bytes!') do @set bytes=%d&@set bytes=000!bytes!&@set bytes=!bytes:~-3!& @set bytes=!zeropad!!bytes!&if "%e" NEQ "" set bytes=!bytes!,%e)& echo !bytes:~-23! %b" | sort /R

EDITED: to display largest to smallest folders

See if this is what you need. d:\files is the target tree here.

@echo off
for /f "delims=" %%a in (' dir "d:\files" /ad /b /s ') do call :size "%%a"
sort /r < "dirsize.tmp"
del "dirsize.tmp"
popd
pause
goto :eof

:size
for /f "tokens=3" %%b in ('dir "%~1" 2^>nul ^|find " File(s) "') do (
for /f "tokens=1-4 delims=," %%c in ("%%b") do (
set dirsize=%%c%%d%%e%%f
)
)
set dirsize=                    %dirsize%
set dirsize=%dirsize:~-20%
>>"dirsize.tmp" echo %dirsize% "%~1"

This is a sample of what I get from it:

        27982878 "d:\images\+ Funny and Odd pictures"
        22595308 "d:\images\+ Mostly Cats\20120917"
        16723196 "d:\images\+ Mostly Cats\20130215"
        10212204 "d:\images\+ Mostly Cats\20121104"
         9177080 "d:\images\+ Mostly Cats\20130506"
         8992465 "d:\images\+ Mostly Cats\20130814"
         8488502 "d:\images\Misc\Good Ideas"
         6985671 "d:\images\Misc\50 Life Hacks"
         5515548 "d:\images\Misc\Photos That Will Make Your Stomach Drop"
         2541431 "d:\images\Misc\Sci Fi"
         2113294 "d:\images\+ Mostly Cats\20130524"
          384100 "d:\images\Astronomy pics\3D"
               0 "d:\images\Misc"
               0 "d:\images\Astronomy pics"
               0 "d:\images\+ Mostly Cats"

This is pretty old question but I don't know why my all friends answer that there is no such command. Here is the command:

dir /o:s

I saw comments that its not working on folders, for the same reason I am editing my answer. This command supports folders too.

I ran this command on my local machine in one folder and below is output

d:\Moody>dir /o:s
 Volume in drive D is Data
 Volume Serial Number is CE51-A3E6

 Directory of d:\Moody

28-Jun-2016  11:22 AM    <DIR>          .
28-Jun-2016  11:22 AM    <DIR>          ..
01-Jun-2016  05:37 PM    <DIR>          binding
05-May-2016  06:29 PM    <DIR>          WMQ 8 JARS
27-Jun-2016  06:51 PM    <DIR>          codebase
27-Jun-2016  05:34 PM    <DIR>          docs
21-Jun-2016  11:12 AM    <DIR>          WMQ 7.5 JARS
18-May-2016  12:56 PM    <DIR>          TestValidation
17-Jun-2016  02:35 PM    <DIR>          java-doc-jms
17-May-2016  04:20 PM    <DIR>          sample-log
20-May-2016  03:28 PM    <DIR>          jms
26-May-2016  12:01 PM    <DIR>          repository
24-Jun-2016  12:09 PM    <DIR>          New folder
05-Apr-2016  04:55 PM    <DIR>          zips
04-May-2016  12:20 PM    <DIR>          PocJms
20-Apr-2016  12:58 PM               901 TestValidation.zip
22-Jun-2016  04:22 PM             7,739 team.xlsx
13-May-2016  09:19 PM             8,700 sample-log.zip
04-Jun-2016  03:06 PM            43,410 Sequence diagrams.mdj
28-Jun-2016  11:22 AM            59,392 FW Binding file for DEV QM.msg
02-May-2016  08:04 PM            81,568 PocJms.zip
21-Mar-2013  01:33 PM            99,926 fscontext-4.2.jar--
22-Jun-2016  02:31 PM           236,631 java-doc-jms.zip
05-May-2016  05:19 PM         7,673,675 WMQ 7.5 JARS.zip
05-May-2016  05:19 PM        12,442,322 WMQ 8 JARS.zip
              15 File(s)     21,028,409 bytes
              19 Dir(s)  73,162,878,976 bytes free

Apart from files which are showing sizes my folders are arranged too. When I check size of binding; its nearly 18 KB; and last folder repository is nearly 1 GB. So it works on folder too.

If you don't Need Date and other info, you can use:

dir /b /a-d /o:s
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top