Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 503 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Folder Size from Windows Command Line

#1
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 the folder in the windows explorer → properties.
Reply

#2
I suggest to download utility DU from the Sysinternals Suite provided by Microsoft at this link

[To see links please register here]


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 © 2005-2011 Mark Russinovich
Sysinternals -

[To see links please register here]


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
Reply

#3
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
Reply

#4
If you have git installed in your computer (getting more and more common) just open MINGW32 and type: `du folder`
Reply

#5
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
Reply

#6
I got `du.exe` with my git distribution. Another place might be aforementioned Microsoft or [Unxutils][1].

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




[1]:

[To see links please register here]

Reply

#7
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.
Reply

#8
I think your only option will be diruse (a highly supported 3rd party solution):

[Get file/directory size from command line][1]

The Windows CLI is unfortuntely quite restrictive, you could alternatively install [Cygwin][2] 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.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#9
::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:**

- Works only with folders of less than 2 GB (cmd does not handle numbers of more than 32 bits)
- Does not read the number of bytes of the internal folders.

Reply

#10
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through