Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

How can I check the size of a folder from the Windows command line?

Writer Matthew Barrera

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

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.

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"
2

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

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

It works on Windows 8.1 without any problems.

1

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

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

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

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

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

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