Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to calculate a Windows folders size?

I need to calculate the size of hundreds of folders, some will be 10MB some maybe 10GB, I need a super fast way of getting the size of each folder using C#.

My end result will hopefully be:

Folder1 10.5GB

Folder2 230MB

Folder3 1.2GB

...

like image 602
LeeW Avatar asked May 19 '10 21:05

LeeW


People also ask

How can I see the folder size quickly?

Right-click on the folder you want to view the size in File Explorer. Select “Properties.” The File Properties dialogue box will appear displaying the folder “Size” and its “Size on disk.” It will also show the file contents of those particular folders.

How get the total size of a folder in Windows?

Go to Windows Explorer and right-click on the file, folder or drive that you're investigating. From the menu that appears, go to Properties. This will show you the total file/drive size. A folder will show you the size in writing, a drive will show you a pie chart to make it easier to see.

How can I see the size of multiple folders?

Here, if you want to check the size of multiple folders, you can press Ctrl key and select all the folders. Then right-click and select Properties. In the pop-up folder properties window, you can see the total size of the selected folders.

Which option is used to find the size of a file or folder?

Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.


2 Answers

Add a reference to the Microsoft Scripting Runtime and use:

Scripting.FileSystemObject fso = new Scripting.FileSystemObject();
Scripting.Folder folder = fso.GetFolder([folder path]);
Int64 dirSize = (Int64)folder.Size;

If you just need the size, this is much faster than recursing.

like image 98
Dave James Avatar answered Oct 22 '22 11:10

Dave James


OK, this is terrible, but...

Use a recursive dos batch file called dirsize.bat:

@ECHO OFF
IF %1x==x GOTO start
IF %1x==DODIRx GOTO dodir
SET CURDIR=%1
FOR /F "usebackq delims=" %%A IN (`%0 DODIR`) DO SET ANSWER=%%A %CURDIR%
ECHO %ANSWER%
GOTO end
:start
FOR /D %%D IN (*.*) DO CALL %0 "%%D"
GOTO end
:dodir
DIR /S/-C %CURDIR% | FIND "File(s)"
GOTO end
:end

Note: There should be a tab character after the final "%%A" on line 5, not spaces.

This is the data you're looking for. It will do thousands of files fairly quickly. In fact, it does my entire harddrive in less than 2 seconds.

Execute the file like this dirsize | sort /R /+25 in order to see the largest directory listed first.

Good luck.

like image 31
BoltBait Avatar answered Oct 22 '22 12:10

BoltBait