64

I want to use the Windows command line to calculate the size of all the files in a folder and subfolder. I would normally do this by right-clicking on the folder and clicking "Properties" but I want to be able to do it on the command line.

Which command can I use?

0

10 Answers 10

55

You will want to use dir /a/s so that it includes every file, including system and hidden files. This will give you the total size you desire.

11
  • 3
    Show me how the output of your command gives me the total size of the directory. Edit your answer to include it. I'll delete my comments and reverse my down vote.
    – dgo
    Commented Dec 1, 2016 at 11:13
  • 3
    @user1167442 You clearly haven't tried it. The output includes the exact same size that explorer properties gives you.
    – DavidPostill
    Commented Dec 7, 2016 at 17:08
  • 4
    @DavidPostill - you are correct. I am hang-doggedly ashamed. I will reverse the downvote. That said, I'm still not too keen on this solution - I'd prefer something a bit faster that doesn't require outputting all the other stuff, but it does work. I have erred. - Ahh nuts - it won;t let me reverse my downvote. As soon as I get the reputation to do so, I will reverse it.
    – dgo
    Commented Dec 8, 2016 at 3:23
  • 1
    It worked - downvote removed -)
    – dgo
    Commented Dec 9, 2016 at 0:04
  • 1
    @QHarr Yes, go ahead and post a question and I'll write up an answer for you. That way, it will help everyone by being searchable for all to find. Commented May 14, 2019 at 16:23
27

You can use PowerShell!

$totalsize = [long]0
Get-ChildItem -File -Recurse -Force -ErrorAction SilentlyContinue | % {$totalsize += $_.Length}
$totalsize

This recurses through the entire current directory (ignoring directories that can't be entered) and sums up the sizes of each file. Then it prints the total size in bytes.

Compacted one-liner:

$totalsize=[long]0;gci -File -r -fo -ea Silent|%{$totalsize+=$_.Length};$totalsize

On my machine, this seems slightly faster than a dir /s /a, since it doesn't print each object's information to the screen.

To run it from a normal command prompt:

powershell -command "$totalsize=[long]0;gci -File -r -fo -ea Silent|%{$totalsize+=$_.Length};$totalsize"
5
  • I wish I spent more time with powershell. I just never like using it for some reason - even though it is super useful for lots of stuff
    – dgo
    Commented Dec 9, 2016 at 14:55
  • Great solution - really quick too!
    – TrojanName
    Commented Feb 18, 2019 at 14:03
  • I get Get-ChildItem : A parameter cannot be found that matches parameter name 'File'
    – golimar
    Commented Jul 18, 2022 at 7:03
  • 2
    @golimar What Windows or PowerShell version do you have? This works on PowerShell 5.1 (standard for Windows 10) and PowerShell 7, but possibly not on older versions that come with older versions of Windows. I think you could instead remove the -File parameter and instead pipe through ? { $_ -is [System.IO.FileInfo] } before piping into the size accumulator.
    – Ben N
    Commented Jul 18, 2022 at 16:17
  • Thanks. It's PS 2.0 on Win7, and it works with that modification
    – golimar
    Commented Jul 19, 2022 at 6:28
12

There is no such command built into DOS or Windows Command Line. On Linux, there’s the du (Disk Usage) command.

Microsoft’s Sysinternals line of tools has a tool that is roughly equivalent to du on Linux. It’s also called du. ;)

2
  • 1
    du -sh <directory> is my go to on Linux (or windows w/ du via git) to show a human readable summary of the directory size.
    – kavun
    Commented Oct 26, 2015 at 15:52
  • Thanks, that's exactly what I needed. As discs get bigger it takes longer and longer to right-click-properties to get file sizes and the result can't be selected and copied into calc to ensure splitting over multiple external discs has written every file in the source folder. I've put that at the end of a robocopy batch piped to a text file then when the batch ends I've got a total file/folder/size metric that can be copy/pasted into calc to ensure the files and bytes are the same as the source. Commented Nov 27, 2019 at 23:26
4

You can still use the command line utility diruse.exe from the Windows 2000 Resource Kit available here:

https://support.microsoft.com/en-us/kb/927229

It works on Windows 8.1 without any problems.

1
  • 2
    Nice find, could you possibly add in an example of usage?
    – Burgi
    Commented Feb 15, 2016 at 13:05
2

dir /s Will list the sizes of all the files and the files in all subfolders

1
  • 1
    I suggest running dir /s /A:-D /d or dir /s /A:-D for a cleaner/clearer view. You can copy cmd.exe's output to something that can highlight the text "File(s) ### bytes" such as vim, gvim, or editpad.org.
    – Rublacava
    Commented Sep 22, 2020 at 0:53
2

The folder size can be calculated with following batch script:

@echo off
setlocal enabledelayedexpansion

set size=0
for /f "tokens=*" %%x in ('dir /s /a /b %1') do set /a size+=%%~zx
echo.!size!

endlocal
3
  • How to run this on Windows 7?
    – klm123
    Commented Sep 25, 2017 at 10:16
  • 2
    this won't work if the total size is over 2GB due to the limit of arithmetic in cmd
    – phuclv
    Commented Mar 11, 2019 at 14:49
  • ...which these days is a pretty serious limitation!
    – rossmcm
    Commented Sep 5, 2019 at 21:07
2

I realize this question asked for file size analysis using CMD line. But if you are open to using PowerQuery (Excel add-in, versions 2010+) then you can create some pretty compelling file size analysis.

The script below can be pasted into a Blank Query; The only thing you'll need to do is add a parameter named "paramRootFolderSearch" then add your value, such as "C:\Users\bl0040\Dropbox\". I used this as a guide: MSSQLTips: Retrieve file sizes from the file system using Power Query.

This query provided the data for me to create a pivot table ([Folder Root]> [Folder Parent (1-2)], [Name]), and I was able to identify a few files that I could deleted which cleared up a lot of space in my directory.

Here is the M script for PowerQuery:

let
// Parmameters:
    valueRootFolderSearch = paramRootFolderSearch,
    lenRootFolderSearch = Text.Length(paramRootFolderSearch),
//

    Source = Folder.Files(paramRootFolderSearch),
    #"Removed Other Columns" = Table.RenameColumns(
Table.SelectColumns(Source,{"Name", "Folder Path", "Attributes"})
,{{"Folder Path", "Folder Path Full"}}),
    #"Expanded Attributes" = Table.ExpandRecordColumn(#"Removed Other Columns", "Attributes", {"Content Type", "Kind", "Size"}, {"Content Type", "Kind", "Size"}),
    #"fx_Size(KB)" = Table.AddColumn(#"Expanded Attributes", "Size(KB)", each [Size]/1024),
    #"fx_Size(MB)" = Table.AddColumn(#"fx_Size(KB)", "Size(MB)", each [Size]/1048576),
    #"fx_Size(GB)" = Table.AddColumn(#"fx_Size(MB)", "Size(GB)", each [Size]/1073741824),
    fx_FolderRoot = Table.AddColumn(#"fx_Size(GB)", "Folder Root", each valueRootFolderSearch),
    helper_LenFolderPathFull = Table.AddColumn(fx_FolderRoot, "LenFolderPathFull", each Text.Length([Folder Path Full])),
    fx_FolderDepth = Table.AddColumn(helper_LenFolderPathFull, "Folder Depth", each Text.End([Folder Path Full], [LenFolderPathFull]-lenRootFolderSearch+1)),
    #"helperList_ListFoldersDepth-Top2" = Table.AddColumn(fx_FolderDepth, "tmp_ListFoldersDepth", each List.Skip(
  List.FirstN(
    List.RemoveNulls(
      Text.Split([Folder Depth],"\")
    )
  ,3)
,1)),
    #"ListFoldersDepth-Top2" = Table.TransformColumns(#"helperList_ListFoldersDepth-Top2", 
{"tmp_ListFoldersDepth", each "\" & Text.Combine(List.Transform(_, Text.From), "\") & "\"
, type text}),
    #"Select Needed Columns" = Table.SelectColumns(#"ListFoldersDepth-Top2",{"Name", "Folder Root", "Folder Depth", "tmp_ListFoldersDepth", "Content Type", "Kind", "Size", "Size(KB)", "Size(MB)", "Size(GB)"}),
    #"rename_FoldersParent(1-2)" = Table.RenameColumns(#"Select Needed Columns",{{"tmp_ListFoldersDepth", "Folders Parent (1-2)"}})
in
    #"rename_FoldersParent(1-2)"

Folder File Sizes_xlsx.png

enter image description here

Folder File Sizes_xlsx2.png

enter image description here

1
  • 1
    I WANTED to like this example, I really did. But you've left out way too many things. Like where /how to paste this query in: Data ribbon > Get Data > From Other Sources > Blank Query then paste it into the white space How to add the parameter: Power Query Editor > Home ribbon > Manage Parameters> New Parameter How to load the query to a worksheet: Power Query Editor > Home ribbon > Close & Load > Close &Load And probably the most important part - how to go from your Data Extract to the summarized Pivot table.
    – grego
    Commented May 4, 2021 at 22:22
2

Microsoft offer a tool called Disk Usage which creates a CSV report.

Du (disk usage) reports the disk space usage for the directory you specify. By default it recurses directories to show the total size of a directory and its subdirectories.

Here is how to use it:

Usage: du [-c[t]] [-l | -n | -v] [-u] [-q] Parameter Description

Where the options are:

-c  Print output as CSV. Use -ct for tab delimiting.
-l  Specify subdirectory depth of information (default is all levels).
-n  Do not recurse.
-v  Show size (in KB) of intermediate directories.
-u  Count each instance of a hardlinked file.
-q  Quiet (no banner).

The CSV output is formatted as:

Path, CurrentFileCount, CurrentFileSize, FileCount, DirectoryCount, DirectorySize

Here is the current official link.

1
-1

Just open power shell and do a du -sh <directory> no need to install 3rd party or sys-internals. Within Power-shell you can run some simple Linux like commands like ls or du commands, some of the switches won't work like ls -alt will error as powershell does not know what the -alt is...

2
  • 5
    What version of PowerShell are you using? And are you sure you don't have Sysinternals installed? The "du" command does not appear to be built in on any systems I have access to. Some documentation link would be helpful too.
    – anlag
    Commented Mar 9, 2018 at 9:50
  • 2
    I'm on the latest fast insider channel of Windows 10 and there's absolutely no du command available
    – phuclv
    Commented Mar 11, 2019 at 14:51
-3

The "dir" command provides file size, last modification date and time of the current directory. First try to move to the directory that you wish to look at the size of using the cd command, then use the dir command.

C:\>dir 

Lists the file size, last modification date and time of all files and directories in the directory that you are currently in, in alphabetical order.

1
  • 4
    This is also a bad answer. This will output the combined size of all the files in the current directory. It will not however output the size of any directories in the current directory. This simply doesn't do what the op is asking.
    – dgo
    Commented Dec 1, 2016 at 4:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .