44

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?

| improve this question | |

10 Answers 10

38

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.

| improve this answer | |
  • 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 Dec 1 '16 at 11:13
  • 2
    @user1167442 You clearly haven't tried it. The output includes the exact same size that explorer properties gives you. – DavidPostill Dec 7 '16 at 17:08
  • 1
    @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 Dec 8 '16 at 3:23
  • 1
    It worked - downvote removed -) – dgo Dec 9 '16 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. – RockPaperLz- Mask it or Casket May 14 '19 at 16:23
18

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"
| improve this answer | |
7

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. ;)

| improve this answer | |
  • 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 Oct 26 '15 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. – Michael Stimson Nov 27 '19 at 23:26
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
| improve this answer | |
2

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.

| improve this answer | |
1

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

| improve this answer | |
1

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.

| improve this answer | |
0

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

| improve this answer | |
  • 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 4 mins ago
0

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...

| improve this answer | |
  • 3
    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 Mar 9 '18 at 9:50
  • I'm on the latest fast insider channel of Windows 10 and there's absolutely no du command available – phuclv Mar 11 '19 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.

| improve this answer | |
  • 3
    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 Dec 1 '16 at 4:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.