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
This code is tested. 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
I got du.exe
with my git distribution. Another place might be aforementioned Microsoft or Unxutils.
Once you got du.exe in your path. Here's 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
Here comes a powershell code I write to list size and file count for all folders under current directory. Feel free to re-use or modify per your need.
$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
}
I recommend to use https://github.com/aleksaan/diskusage utility. Very simple and helpful. And very fast.
Just type in a command shell
diskusage.exe -path 'd:/go; d:/Books'
and get list of folders arranged 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 executed at 272ms on HDD.
You can increase depth of subfolders to analyze, for example:
diskusage.exe -path 'd:/go; d:/Books' -depth 2
and get sizes not only for 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 has been scanned for 3m12s
I think your only option will be diruse (a highly supported 3rd party solution):
Get file/directory size from command line
The Windows CLI is unfortuntely quite restrictive, you could alternatively install Cygwin which is a dream to use compared to cmd. That would give you access to the ported Unix tool du which is the basis of diruse on windows.
Sorry I wasn't able to answer your questions directly with a command you can run on the native cli.