13

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!

Julian Laval
  • 1,210
  • 4
  • 17
  • 34

5 Answers5

16

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
sparks
  • 271
  • 2
  • 3
  • Perfect one line solution! – Kavinda Jayakody Mar 26 '20 at 19:23
  • It's usually a bad idea to run long commands that you have little idea what they're actually doing in the command line... but this one worked fine for my purposes. Thanks, way better than downloading some program to do it for me. – Bilbo Baggins Dec 29 '22 at 22:38
9

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"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Thanks! A few questions: 1) How could I get the size in bytes? The GB approximation was unnecessary, I meant to use it as an example of the file structure I was looking for. 2) Whilst running the above code, I observed that folders that were in fact MB were displayed as GB (e.g. 659MB was shown as 659GB). Just something I thought was worth noting. – Julian Laval Aug 24 '13 at 12:00
  • 1
    Yes, it was cocked up. I've edited it to report bytes and if you want to remove the commas then remove the `REM` at the start of the line. – foxidrive Aug 24 '13 at 12:30
2

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"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I get the error `%%a was unexpected at this time`. Likewise for the other variables. Any ideas? – Julian Laval Aug 19 '13 at 10:41
  • How did you run it? What did you put in the place of `d:\files` ? It does work here. I suspect you pasted it into a cmd prompt - but it is a batch file. – foxidrive Aug 19 '13 at 13:57
  • Ah, I should indeed have run it as a batch file! However, it still doesn't seem to order the subfolders by size... – Julian Laval Aug 19 '13 at 19:19
  • Oh yes, I read your question too quickly. Try my edited code. – foxidrive Aug 19 '13 at 22:34
  • The modified code doesn't seem to solve the issue. I revised my question to better explain myself. Thank you infinitely for your help! – Julian Laval Aug 20 '13 at 23:05
  • Is the problem that you want only GB sizes listed? See my edit above for what I see when I use it on my drive. Is that too different to what you need? **Edit:** I think that you want totals of each folder **with** subfolders, not just the folder itself. Do you want totals at only one folder level? You listed `folder 1` and ignored `Subfolder 1` for example. – foxidrive Aug 21 '13 at 07:11
  • Your other answer does the job perfectly! Thank you very much! – Julian Laval Aug 24 '13 at 11:56
1

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.

shaILU
  • 2,050
  • 3
  • 21
  • 40
  • Check this for details: http://how-to-use-dos.blogspot.in/2008/07/dos-commands-dir.html – shaILU Oct 30 '15 at 07:17
  • 1
    This doesnt work for folders, just for files. Your friends are right. – katzenhut Jun 25 '16 at 15:42
  • No @katzenhut ..... check this. In one of my dir command dir /o:s gives below: -----OUTPUT----- 01-Jun-2016 05:37 PM binding >>>removed lines as ans become too long 04-May-2016 12:20 PM PocJms 26-May-2016 12:01 PM repository 20-Apr-2016 12:58 PM 901 TestValidation.zip >>>removed lines as ans become too long 05-May-2016 05:19 PM 12,442,322 WMQ 8 JARS.zip -------------- As mentioned in output directory binding is of size 18 KB and repository ~1GB, so it arranges subdir as well. Please check. – shaILU Jun 27 '16 at 08:34
  • @katzenhut, included. Please check. And +1 if you feel ok. – shaILU Jun 28 '16 at 06:41
  • 1
    i checked. it doesnt work for me. files are ordered, but folders aren't. judging from the question, it doesn't work for the op either. no idea why it seems to work for you. – katzenhut Jun 28 '16 at 09:48
  • I tried on Win 7 Professional Service Pack 1. Does this make difference? – shaILU Jun 28 '16 at 17:24
  • This answer just displays the folders in alphabetical order - which isn't what the original poster wanted. – Richard Apr 30 '18 at 17:58
  • Richard in my output folder are not in alphabetical order. Can you check and if its ok give +1 back – shaILU Jun 19 '19 at 16:01
-2

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

dir /b /a-d /o:s
Ibrahim Akbar
  • 119
  • 1
  • 8