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
...
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.
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.
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.
Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With