Windows Dir 命令 - 按大小排序子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18302537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Dir command - order subdirectories by size
提问by Julian Laval
How could I display the subdirectories of a folder from largest to smallest using the dir command?
如何使用 dir 命令从大到小显示文件夹的子目录?
I've tried using dir /O:-S
command, and although it sorts files just fine, it doesn't seem to order the subdirectories.
我试过使用dir /O:-S
命令,虽然它对文件进行了很好的排序,但它似乎没有对子目录进行排序。
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?
假设文件夹 1(包括其子文件夹中的所有文件)的总大小为 10GB,文件夹 2 的总大小为 15GB,我将如何按总内容大小排序输出它们的顺序?
I.e.
IE
94932485 Folder 2
6453445 Folder 1
Thanks in advance!
提前致谢!
采纳答案by foxidrive
This seems to work for the changed requirements: alter c:\folder
to the folder level you want to query.
这似乎适用于更改后的要求:更改c:\folder
您要查询的文件夹级别。
@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"
回答by sparks
If you are after a one-line solution, that supports upto 999 terabytes:
如果您追求的是单线解决方案,它最多支持 999 TB:
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
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
回答by foxidrive
EDITED: to display largest to smallest folders
编辑:显示最大到最小的文件夹
See if this is what you need. d:\files
is the target tree here.
看看这是否是您所需要的。 d:\files
是这里的目标树。
@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 Cats120917"
16723196 "d:\images\+ Mostly Cats130215"
10212204 "d:\images\+ Mostly Cats121104"
9177080 "d:\images\+ Mostly Cats130506"
8992465 "d:\images\+ Mostly Cats130814"
8488502 "d:\images\Misc\Good Ideas"
6985671 "d:\images\Misc Life Hacks"
5515548 "d:\images\Misc\Photos That Will Make Your Stomach Drop"
2541431 "d:\images\Misc\Sci Fi"
2113294 "d:\images\+ Mostly Cats130524"
384100 "d:\images\Astronomy picsD"
0 "d:\images\Misc"
0 "d:\images\Astronomy pics"
0 "d:\images\+ Mostly Cats"
回答by shaILU
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
目录/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.
除了显示大小的文件外,我的文件夹也被安排了。在检查装订的尺寸的时候;将近 18 KB;最后一个文件夹存储库接近 1 GB。所以它也适用于文件夹。
回答by Ibrahim Akbar
If you don't Need Date and other info, you can use:
如果您不需要日期和其他信息,您可以使用:
dir /b /a-d /o:s