Is it possible in Windows to get a folder\'s size from the command line without using any 3rd party tool?
I want the same result as you would get when right clicking
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).
GetDirSummary.bat Script:
@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
Output:
Total Files Listed:
22 File(s) 63,600 bytes
8 Dir(s) 104,350,330,880 bytes free
There is a built-in Windows tool for that:
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
.
I suggest to download utility DU from the Sysinternals Suite provided by Microsoft at this link 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
The following script can be used to fetch and accumulate the size of each file under a given folder.
The folder path %folder%
can be given as an argument to this script (%1
).
Ultimately, the results is held in the parameter %filesize%
@echo off
SET count=1
SET foldersize=0
FOR /f "tokens=*" %%F IN ('dir /s/b %folder%') DO (call :calcAccSize "%%F")
echo %filesize%
GOTO :eof
:calcAccSize
REM echo %count%:%1
REM set /a count+=1
set /a foldersize+=%~z1
GOTO :eof
Note: The method calcAccSize
can also print the content of the folder (commented in the example above)
I solved similar problem. Some of methods in this page are slow and some are problematic in multilanguage environment (all suppose english). I found simple workaround using vbscript in cmd. It is tested in W2012R2 and W7.
>%TEMP%\_SFSTMP$.VBS ECHO/Set objFSO = CreateObject("Scripting.FileSystemObject"):Set objFolder = objFSO.GetFolder(%1):WScript.Echo objFolder.Size
FOR /F %%? IN ('CSCRIPT //NOLOGO %TEMP%\_SFSTMP$.VBS') DO (SET "S_=%%?"&&(DEL %TEMP%\_SFSTMP$.VBS))
It set environment variable S_. You can, of course, change last line to directly display result to e.g.
FOR /F %%? IN ('CSCRIPT //NOLOGO %TEMP%\_SFSTMP$.VBS') DO (ECHO "Size of %1 is %%?"&&(DEL %TEMP%\_SFSTMP$.VBS))
You can use it as subroutine or as standlone cmd. Parameter is name of tested folder closed in quotes.
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 cmd
is 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).
An alternative is PowerShell:
Get-ChildItem -Recurse | Measure-Object -Sum Length
or shorter:
ls -r | measure -sum Length
If you want it prettier:
switch((ls -r|measure -sum 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
:
powershell -noprofile -command "ls -r|measure -sum Length"
1 I 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 :-)