从 Windows 命令行获取文件夹大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12813826/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:58:14  来源:igfitidea点击:

Get Folder Size from Windows Command Line

windowscommand-line

提问by Eldad Assis

Is it possible in Windows to get a folder's size from the command line without using any 3rd party tool?

在 Windows 中是否可以在不使用任何 3rd 方工具的情况下从命令行获取文件夹的大小?

I want the same result as you would get when right clicking the folder in the windows explorer → properties.

我想要的结果与右键单击 Windows 资源管理器 → 属性中的文件夹时得到的结果相同。

采纳答案by Joey

You can just add up sizes recursively (the following is a batch file):

您可以递归地添加大小(以下是批处理文件):

@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %size% Bytes

However, this has several problems because cmdis limited to 32-bit signed integer arithmetic. So it will get sizes above 2 GiB wrong1. Furthermore it will likely count symlinks and junctions multiple times so it's at best an upper bound, not the true size (you'll have that problem with any tool, though).

但是,这有几个问题,因为cmd仅限于 32 位有符号整数算法。所以它的大小会超过 2 GiB 错误1。此外,它可能会多次计算符号链接和连接点,因此它充其量只是一个上限,而不是真正的大小(尽管使用任何工具都会遇到这个问题)。

An alternative is PowerShell:

另一种选择是 PowerShell:

Get-ChildItem -Recurse | Measure-Object -Sum Length

or shorter:

或更短:

ls -r | measure -s Length

If you want it prettier:

如果你想让它更漂亮:

switch((ls -r|measure -s Length).Sum) {
  {$_ -gt 1GB} {
    '{0:0.0} GiB' -f ($_/1GB)
    break
  }
  {$_ -gt 1MB} {
    '{0:0.0} MiB' -f ($_/1MB)
    break
  }
  {$_ -gt 1KB} {
    '{0:0.0} KiB' -f ($_/1KB)
    break
  }
  default { "$_ bytes" }
}

You can use this directly from cmd:

您可以直接从cmd以下位置使用它:

powershell -noprofile -command "ls -r|measure -s Length"


1I do have a partially-finished bignum library in batch files somewhere which at least gets arbitrary-precision integer addition right. I should really release it, I guess :-)

1我在批处理文件中确实有一个部分完成的 bignum 库,它至少可以正确地获得任意精度的整数加法。我真的应该发布它,我想:-)

回答by Nir Duan

There is a built-in Windows toolfor that:

有一个内置的 Windows 工具

dir /s 'FolderName'

This will print a lot of unnecessary information but the end will be the folder size like this:

这将打印很多不必要的信息,但最终将是这样的文件夹大小:

 Total Files Listed:
       12468 File(s)    182,236,556 bytes

If you need to include hidden folders add /a.

如果您需要包含隐藏文件夹,请添加/a.

回答by Steve

I suggest to download utility DU from the Sysinternals Suite provided by Microsoft at this link http://technet.microsoft.com/en-us/sysinternals/bb896651

我建议从 Microsoft 提供的 Sysinternals Suite 下载实用程序 DU,网址为 http://technet.microsoft.com/en-us/sysinternals/bb896651

usage: du [-c] [-l <levels> | -n | -v] [-u] [-q] <directory>
   -c     Print output as CSV.
   -l     Specify subdirectory depth of information (default is all levels).
   -n     Do not recurse.
   -q     Quiet (no banner).
   -u     Count each instance of a hardlinked file.
   -v     Show size (in KB) of intermediate directories.


C:\SysInternals>du -n d:\temp

Du v1.4 - report directory disk usage
Copyright (C) 2005-2011 Mark Russinovich
Sysinternals - www.sysinternals.com

Files:        26
Directories:  14
Size:         28.873.005 bytes
Size on disk: 29.024.256 bytes

While you are at it, take a look at the other utilities. They are a life-saver for every Windows Professional

当您在使用它时,请查看其他实用程序。它们是每个 Windows Professional 的救星

回答by frizik

Oneliner:

单线:

powershell -command "$fso = new-object -com Scripting.FileSystemObject; gci -Directory | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName | sort Size -Descending | ft @{l='Size [MB]'; e={'{0:N2}    ' -f ($_.Size / 1MB)}},FullName"

Same but Powershell only:

相同但仅限 Powershell:

$fso = new-object -com Scripting.FileSystemObject
gci -Directory `
  | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName `
  | sort Size -Descending `
  | ft @{l='Size [MB]'; e={'{0:N2}    ' -f ($_.Size / 1MB)}},FullName

This should produce the following result:

这应该产生以下结果:

Size [MB]  FullName
---------  --------
580,08     C:\my\Tools\mongo
434,65     C:\my\Tools\Cmder
421,64     C:\my\Tools\mingw64
247,10     C:\my\Tools\dotnet-rc4
218,12     C:\my\Tools\ResharperCLT
200,44     C:\my\Tools\git
156,07     C:\my\Tools\dotnet
140,67     C:\my\Tools\vscode
97,33      C:\my\Tools\apache-jmeter-3.1
54,39      C:\my\Tools\mongoadmin
47,89      C:\my\Tools\Python27
35,22      C:\my\Tools\robomongo

回答by Custodio

If you have git installed in your computer (getting more and more common) just open MINGW32 and type: du folder

如果您的计算机中安装了 git(越来越普遍),只需打开 MINGW32 并键入: du folder

回答by Alexander Anufriev

I recommend to use https://github.com/aleksaan/diskusageutility. Very simple and helpful. And very fast.

我建议使用https://github.com/aleksaan/diskusage实用程序。非常简单和乐于助人。而且非常快。

Just type in a command shell

只需输入命令外壳

diskusage.exe -path 'd:/go; d:/Books'

and get list of folders arranged by size

并获取按大小排列的文件夹列表

  1.| DIR: d:/go      | SIZE: 325.72 Mb   | DEPTH: 1 
  2.| DIR: d:/Books   | SIZE:  14.01 Mb   | DEPTH: 1 

This example was executed at 272mson HDD.

此示例在 HDD 上以272 毫秒执行。

You can increase depth of subfolders to analyze, for example:

您可以增加要分析的子文件夹的深度,例如:

diskusage.exe -path 'd:/go; d:/Books' -depth 2

and get sizes not only for selected folders but also for its subfolders

不仅可以获取选定文件夹的大小,还可以获取其子文件夹的大小

  1.| DIR: d:/go            | SIZE: 325.72 Mb   | DEPTH: 1 
  2.| DIR: d:/go/pkg        | SIZE: 212.88 Mb   | DEPTH: 2 
  3.| DIR: d:/go/src        | SIZE:  62.57 Mb   | DEPTH: 2 
  4.| DIR: d:/go/bin        | SIZE:  30.44 Mb   | DEPTH: 2 
  5.| DIR: d:/Books/Chess   | SIZE:  14.01 Mb   | DEPTH: 2 
  6.| DIR: d:/Books         | SIZE:  14.01 Mb   | DEPTH: 1 
  7.| DIR: d:/go/api        | SIZE:   6.41 Mb   | DEPTH: 2 
  8.| DIR: d:/go/test       | SIZE:   5.11 Mb   | DEPTH: 2 
  9.| DIR: d:/go/doc        | SIZE:   4.00 Mb   | DEPTH: 2 
 10.| DIR: d:/go/misc       | SIZE:   3.82 Mb   | DEPTH: 2 
 11.| DIR: d:/go/lib        | SIZE: 358.25 Kb   | DEPTH: 2 

* 3.5Tb on the server has been scanned for 3m12s

* 服务器上的 3.5Tb 已扫描 3m12s

回答by Ryan Lee

Here comes a powershell code I write to list size and file count for all folders under current directory. Feel free to re-use or modify per your need.

这是我编写的 powershell 代码,用于列出当前目录下所有文件夹的大小和文件数。您可以根据需要随意重复使用或修改。

$FolderList = Get-ChildItem -Directory
foreach ($folder in $FolderList)
{
    set-location $folder.FullName
    $size = Get-ChildItem -Recurse | Measure-Object -Sum Length
    $info = $folder.FullName + "    FileCount: " + $size.Count.ToString() + "   Size: " + [math]::Round(($size.Sum / 1GB),4).ToString() + " GB"
    write-host $info
}

回答by Chienvela

This code is tested. You can check it again.

此代码经过测试。你可以再检查一下。

@ECHO OFF
CLS
SETLOCAL
::Get a number of lines contain "File(s)" to a mytmp file in TEMP location.
DIR /S /-C | FIND "bytes" | FIND /V "free" | FIND /C "File(s)" >%TEMP%\mytmp
SET /P nline=<%TEMP%\mytmp
SET nline=[%nline%]
::-------------------------------------
DIR /S /-C | FIND "bytes" | FIND /V "free" | FIND /N "File(s)" | FIND "%nline%" >%TEMP%\mytmp1
SET /P mainline=<%TEMP%\mytmp1
CALL SET size=%mainline:~29,15%
ECHO %size%
ENDLOCAL
PAUSE

回答by Steve

I guess this would only work if the directory is fairly static and its contents don't change between the execution of the two dir commands. Maybe a way to combine this into one command to avoid that, but this worked for my purpose (I didn't want the full listing; just the summary).

我想这只有在目录相当静态并且其内容在两个 dir 命令的执行之间没有变化时才有效。也许是一种将其组合成一个命令以避免这种情况的方法,但这对我的目的有效(我不想要完整的列表;只是摘要)。

GetDirSummary.bat Script:

GetDirSummary.bat 脚本:

@echo off
rem  get total number of lines from dir output
FOR /F "delims=" %%i IN ('dir /S %1 ^| find "asdfasdfasdf" /C /V') DO set lineCount=%%i
rem  dir summary is always last 3 lines; calculate starting line of summary info
set /a summaryStart="lineCount-3"
rem  now output just the last 3 lines
dir /S %1 | more +%summaryStart%

Usage:

用法:

GetDirSummary.bat c:\temp

GetDirSummary.bat c:\temp

Output:

输出:

 Total Files Listed:
          22 File(s)         63,600 bytes
           8 Dir(s)  104,350,330,880 bytes free

回答by Gustavo Davico

::Get a number of lines that Dir commands returns (/-c to eliminate number separators: . ,) ["Tokens = 3" to look only at the third column of each line in Dir]

::获取 Dir 命令返回的行数(/-c 以消除数字分隔符:. ,)["Tokens = 3" 仅查看 Dir 中每行的第三列]

FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO set /a i=!i!+1

FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO set /a i=!i!+1

Number of the penultimate line, where is the number of bytes of the sum of files:

倒数第二行,其中是文件总和的字节数:

set /a line=%i%-1

Finally get the number of bytes in the penultimate line - 3rd column:

最后获得倒数第二行的字节数 - 第三列:

set i=0
FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO (
  set /a i=!i!+1
  set bytes=%%a
  If !i!==%line% goto :size  
)
:size
echo %bytes%

As it does not use word search it would not have language problems.

由于它不使用单词搜索,因此不会有语言问题。

Limitations:

限制:

  • Works only with folders of less than 2 GB (cmd does not handle numbers of more than 32 bits)
  • Does not read the number of bytes of the internal folders.
  • 仅适用于小于 2 GB 的文件夹(cmd 不处理超过 32 位的数字)
  • 不读取内部文件夹的字节数。