30

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?

30

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.

  • 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. – RockPaperLizard May 14 at 16:23
9

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"
  • 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 Dec 9 '16 at 14:55
  • Great solution - really quick too! – TrojanName Feb 18 at 14:03
6

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

  • 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
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
  • How to run this on Windows 7? – klm123 Sep 25 '17 at 10:16
  • this won't work if the total size is over 2GB due to the limit of arithmetic in cmd – phuclv Mar 11 at 14:49
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.

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

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

0

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.

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

  • 2
    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 at 14:51
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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