Page 1 of 1
Batch File To Display Directory Size
Posted: 06 Jul 2009 10:40
by paulo
I grabbed the below code from:
http://stackoverflow.com/questions/7594 ... ctory-size
Code: Select all
@echo off
setLocal EnableDelayedExpansion
set /a sum=0
for /d %%D in (%1) do (
FOR /R %%D %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! k
When I try to run this code I get the
%I was unexpected at this time error message. How I can get this code working?
Posted: 06 Jul 2009 11:04
by RElliott63
Remove the %%D from the 2nd FOR Loop .. .as in:
Code: Select all
@echo off
setLocal EnableDelayedExpansion
set /a sum=0
FOR /d %%D in (%1) do (
FOR /R %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! k
Posted: 06 Jul 2009 15:12
by paulo
Well... This works, but not as expected. The nested loop iterates the command-line current directory, not the top-level
for directory. :S
I also tried this, but doesn't work too:
Code: Select all
@echo off
setLocal EnableDelayedExpansion
set /a sum=0
FOR /d %%D in (%1) do (
FOR /R %%I IN (%%D\*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! k
Any idea? Thanks anyway.
Posted: 06 Jul 2009 18:23
by ghostmachine4
forget that. you can use vbscript instead
Code: Select all
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\test") ' folder to scan for size
WScript.Echo objFolder.Size
save the above as dirsize.vbs and on command line
Posted: 06 Jul 2009 21:47
by RElliott63
Paulo,
%1 should be the directory you want to run the summation for...
You could do this:
DirSize "C:\FolderName"
Code: Select all
@echo off
setLocal EnableDelayedExpansion
If /i [%~1] equ [] (
Cls
Echo Gotta have a Folder to calculate!
Goto Exit
)
set Fldr="%~1"
set /a sum=0
FOR /D %%D in ('Dir /b %Fldr%') do (
FOR /R %%I IN (%%D\*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! k
:exit
If you are wanting this to traverse sub-folders... add the "/s" to the DIR command.
If you are wanting to just see the FOLDERS only... add the "/ad" to the DIR command.
*Disclaimer* This might need some tweaking based on whether or not you have spaces in your folder names, etc.