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
::Get a number of lines that Dir commands returns (/-c to eliminate number separators: . ,) ["Tokens = 3" to look only at the third column of each line in Dir]
FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO set /a i=!i!+1
Number of the penultimate line, where is the number of bytes of the sum of files:
set /a line=%i%-1
Finally get the number of bytes in the penultimate line - 3rd column:
set i=0
FOR /F "tokens=3" %%a IN ('dir /-c "%folderpath%"') DO (
set /a i=!i!+1
set bytes=%%a
If !i!==%line% goto :size
)
:size
echo %bytes%
As it does not use word search it would not have language problems.
Limitations:
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 whatever folder you want and change %A to %%A if using in a batch file
It returns the size of the whole folder, including subfolders and hidden and system files, and works with folders over 2GB
It does write to the screen, so you'll have to use an interim file if you don't want that.
Easiest method to get just the total size is powershell, but still is limited by fact that pathnames longer than 260 characters are not included in the total
So here is a solution for both your requests in the manner you originally asked for. It will give human readability filesize without the filesize limits everyone is experiencing. Compatible with Win Vista or newer. XP only available if Robocopy is installed. Just drop a folder on this batch file or use the better 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 conveniently put it in your SendTo folder. This will allow you to right click a folder or selection of folders, click on the SendTo option, and then select this batch file.
To find the SendTo folder on your computer simplest way is to open up cmd then copy in this line as is.
explorer C:\Users\%username%\AppData\Roaming\Microsoft\Windows\SendTo
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 but Powershell only:
$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 produce 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
If you have git installed in your computer (getting more and more common) just open MINGW32 and type: du folder