Get folder size from windows command prompt

Is it possible in Windows to get the size of a folder from the command line without using any third-party tool?

I want to get the same result as when I right-click on a folder in Windows Explorer. Properties

+112
command-line windows
Oct 10 '12 at 7:08
source share
17 answers

You can simply add dimensions recursively (the following 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 limited to 32-bit signed integer arithmetic. Thus, it will have sizes above 2 GB incorrectly 1 . In addition, it will probably count symbolic links and transitions several times, so at best it’s the upper bound and not the true size (you will have this problem with any tool).

An alternative is PowerShell:

 Get-ChildItem -Recurse | Measure-Object -Sum Length 

or shorter:

 ls -r | measure -s Length 

If you want it more beautiful:

 switch((ls -r|measure -s 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 -s Length" 



1 I have a partially processed bignum library in batch files somewhere that at least has the right to an arbitrary number of integers. I really have to let him go, I think :-)

+108
Oct 10 '12 at 7:16
source share

There is a built-in Windows tool for this:

 dir /s 'FolderName' 

This will print a lot of unnecessary information, but in the end there will be a folder size similar to this:

  Total Files Listed: 12468 File(s) 182,236,556 bytes 

If you need to enable hidden folders, add /a .

+101
Oct 26 '16 at 7:47
source share

I suggest downloading the DU utility from Sysinternals Suite, provided by Microsoft from 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 -nd:\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're on it, take a look at other utilities. They are vital to every Windows Professional.

+62
Oct 10 '12 at 7:16
source share

Oneliner:

 powershell -command "$fso = new-object -com Scripting.FileSystemObject; gci -Directory | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName | sort Size -Descending | ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName" 

Same as Powershell:

 $fso = new-object -com Scripting.FileSystemObject gci -Directory ` | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName ` | sort Size -Descending ` | ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName 

This should lead to the following result:

 Size [MB] FullName --------- -------- 580,08 C:\my\Tools\mongo 434,65 C:\my\Tools\Cmder 421,64 C:\my\Tools\mingw64 247,10 C:\my\Tools\dotnet-rc4 218,12 C:\my\Tools\ResharperCLT 200,44 C:\my\Tools\git 156,07 C:\my\Tools\dotnet 140,67 C:\my\Tools\vscode 97,33 C:\my\Tools\apache-jmeter-3.1 54,39 C:\my\Tools\mongoadmin 47,89 C:\my\Tools\Python27 35,22 C:\my\Tools\robomongo 
+32
Dec 19 '17 at 21:09 on
source share

If you have git installed on your computer (becoming more common) just open MINGW32 and type: du folder

+11
May 08 '14 at 14:02
source share

I recommend using the https://github.com/aleksaan/diskusage utility. Very simple and helpful. And very fast.

Just type in command shell

 diskusage.exe -path 'd:/go; d:/Books' 

and get a list of folders sorted by size

   1. |  DIR: d: / go |  SIZE: 325.72 Mb |  DEPTH: 1 
   2. |  DIR: d: / Books |  SIZE: 14.01 Mb |  DEPTH: 1 

This example was run at 272ms on the HDD.

You can increase the depth of the subfolders for analysis, for example:

 diskusage.exe -path 'd:/go; d:/Books' -depth 2 

and get the sizes not only for the selected folders, but also for its subfolders

   1. |  DIR: d: / go |  SIZE: 325.72 Mb |  DEPTH: 1 
   2. |  DIR: d: / go / pkg |  SIZE: 212.88 Mb |  DEPTH: 2 
   3. |  DIR: d: / go / src |  SIZE: 62.57 Mb |  DEPTH: 2 
   4. |  DIR: d: / go / bin |  SIZE: 30.44 Mb |  DEPTH: 2 
   5. |  DIR: d: / Books / Chess |  SIZE: 14.01 Mb |  DEPTH: 2 
   6. |  DIR: d: / Books |  SIZE: 14.01 Mb |  DEPTH: 1 
   7. |  DIR: d: / go / api |  SIZE: 6.41 Mb |  DEPTH: 2 
   8. |  DIR: d: / go / test |  SIZE: 5.11 Mb |  DEPTH: 2 
   9. |  DIR: d: / go / doc |  SIZE: 4.00 Mb |  DEPTH: 2 
  10. |  DIR: d: / go / misc |  SIZE: 3.82 Mb |  DEPTH: 2 
  11. |  DIR: d: / go / lib |  SIZE: 358.25 Kb |  DEPTH: 2 

* 3.5Tb on the server was tested on 3m12s

+7
Aug 24 '18 at 16:17
source share

Here is the PowerShell code that I write for the size of the list and the number of files for all folders in the current directory. Feel free to reuse or modify to suit your needs.

 $FolderList = Get-ChildItem -Directory foreach ($folder in $FolderList) { set-location $folder.FullName $size = Get-ChildItem -Recurse | Measure-Object -Sum Length $info = $folder.FullName + " FileCount: " + $size.Count.ToString() + " Size: " + [math]::Round(($size.Sum / 1GB),4).ToString() + " GB" write-host $info } 
+3
Oct 09 '18 at 4:56
source share

This code has been verified. You can check it again.

 @ECHO OFF CLS SETLOCAL ::Get a number of lines contain "File(s)" to a mytmp file in TEMP location. DIR /S /-C | FIND "bytes" | FIND /V "free" | FIND /C "File(s)" >%TEMP%\mytmp SET /P nline=<%TEMP%\mytmp SET nline=[%nline%] ::------------------------------------- DIR /S /-C | FIND "bytes" | FIND /V "free" | FIND /N "File(s)" | FIND "%nline%" >%TEMP%\mytmp1 SET /P mainline=<%TEMP%\mytmp1 CALL SET size=%mainline:~29,15% ECHO %size% ENDLOCAL PAUSE 
+2
Aug 10 '13 at 14:37
source share

I assume that this will only work if the directory is fairly static and its contents do not change between the execution of two dir commands. Perhaps this is a way to combine this into one command to avoid this, but it worked for my purpose (I did not want the complete list, just a 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% 

Using:

GetDirSummary.bat c: \ temp

Output:

  Total Files Listed: 22 File(s) 63,600 bytes 8 Dir(s) 104,350,330,880 bytes free 
+2
Dec 06 '17 at 19:41
source share

I think your only option would be diruse (a very supported third-party solution):

Get file / directory size from command line

The Windows CLI is unfortunately quite restrictive; you can alternatively install Cygwin , which is a dream to use compared to cmd. This will give you access to the ported Unix du tool, which underlies the diruse on windows.

Sorry that I could not answer your questions directly with a team that you can run on your native Kli.

+1
Oct 10 '12 at 7:22
source share

I got du.exe with my git distribution. Another place may be the aforementioned Microsoft or Unhutils .

Once you get the du.exe file in your path. Here is your fileSizes.bat

 @echo ___________ @echo DIRECTORIES @for /D %%i in (*) do @CALL du.exe -hs "%%i" @echo _____ @echo FILES @for %%i in (*) do @CALL du.exe -hs "%%i" @echo _____ @echo TOTAL @du.exe -sh "%CD%" 

βžͺ

 ___________ DIRECTORIES 37M Alps-images 12M testfolder _____ FILES 765K Dobbiaco.jpg 1.0K testfile.txt _____ TOTAL 58M D:\pictures\sample 
+1
Oct 24 '15 at 17:13
source share

:: Get some rows returned by Dir commands (/ -c to remove the number separators:.,) ["Tokens = 3" look only at the third column of each row in Dir]

FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO set /ai=!i!+1

Number of the penultimate line, where the number of bytes of the sum of files is indicated:

 set /a line=%i%-1 

Finally, we get the number of bytes in the penultimate row - the third column:

 set i=0 FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO ( set /ai=!i!+1 set bytes=%%a If !i!==%line% goto :size ) :size echo %bytes% 

Since he does not use word search, he will not have language problems.

Limitations:

  • Only works with folders smaller than 2 GB (cmd does not process numbers larger than 32 bits).
  • Does not read the number of bytes of internal folders.
+1
Jun 02 '17 at 14:08
source share

Try:

 SET FOLDERSIZE=0 FOR /F "tokens=3" %A IN ('DIR "C:\Program Files" /a /-c /s ^| FINDSTR /C:" bytes" ^| FINDSTR /V /C:" bytes free"') DO SET FOLDERSIZE=%A 

Change C: \ Program Files to any desired folder and change% A to %% A if it is used in a batch file

It returns the size of the entire folder, including subfolders and hidden and system files, and works with folders over 2 GB

It writes to the screen, so you have to use an intermediate file if you do not want it.

+1
Nov 14 '17 at 8:47
source share

The following script can be used to get and accumulate the size of each file in a given folder.
The path to %folder% can be specified as an argument to this script ( %1 ).
Ultimately, the results are saved in the %filesize% parameter

 @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: calcAccSize method calcAccSize also print the contents of a folder (commented on in the example above)

+1
Mar 25 '19 at 8:40
source share

So, here is the solution for your queries the way you originally requested. This will ensure file readability with no file size restrictions that everyone encounters. Compatible with Win Vista or later. XP is only available if Robocopy is installed. Just put the folder in this batch file or use the best method mentioned below.

 @echo off setlocal enabledelayedexpansion set "vSearch=Files :" For %%i in (%*) do ( set "vSearch=Files :" For /l %%M in (1,1,2) do ( for /f "usebackq tokens=3,4 delims= " %%A in ('Robocopy "%%i" "%%i" /E /L /NP /NDL /NFL ^| find "!vSearch!"') do ( if /i "%%M"=="1" ( set "filecount=%%A" set "vSearch=Bytes :" ) else ( set "foldersize=%%A%%B" ) ) ) echo Folder: %%~nxi FileCount: !filecount! Foldersize: !foldersize! REM remove the word "REM" from line below to output to txt file REM echo Folder: %%~nxi FileCount: !filecount! Foldersize: !foldersize!>>Folder_FileCountandSize.txt ) pause 

To be able to use this batch file, place it in the Submit folder. This will allow you to right-click the folder or selected folders, select the β€œSend” option, and then select this batch file.

To find the SendTo folder on your computer, the easiest way is to open cmd and copy it to this line as is.

 explorer C:\Users\%username%\AppData\Roaming\Microsoft\Windows\SendTo 
+1
May 15 '19 at 5:26
source share

I solved a similar problem. Some of the methods on this page are slow, and some of them are problematic in a multilingual environment (all assume English). I found a 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 sets the environment variable S_. You can, of course, change the last line to directly display the result, for example,

 FOR /F %%? IN ('CSCRIPT //NOLOGO %TEMP%\_SFSTMP$.VBS') DO (ECHO "Size of %1 is %%?"&&(DEL %TEMP%\_SFSTMP$.VBS)) 

You can use it as a subroutine or as a separate cmd. Parameter - the name of the tested folder, enclosed in quotation marks.

0
Mar 19 '16 at 13:49
source share

The easiest way to get only the total size is powershell, but still limited by the fact that path names longer than 260 characters are not included in the general

-3
02 feb. '15 at 19:22
source share



All Articles