Batch File To Display Directory Size

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
paulo
Posts: 3
Joined: 06 Jul 2009 08:20

Batch File To Display Directory Size

#1 Post by paulo » 06 Jul 2009 10:40

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?

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 06 Jul 2009 11:04

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

paulo
Posts: 3
Joined: 06 Jul 2009 08:20

#3 Post by paulo » 06 Jul 2009 15:12

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. :)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#4 Post by ghostmachine4 » 06 Jul 2009 18:23

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

Code: Select all

e:\> cscript /nologo dirsize.vbs

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#5 Post by RElliott63 » 06 Jul 2009 21:47

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.

Post Reply