DOS batch file to recursively find the size of each directory

17 views
Skip to first unread message

bha...@lycos.com

unread,
Jan 18, 2014, 11:33:35 AM1/18/14
to
Greetings,

http://www.bish.co.uk/forum/index.php?topic=58.0 provides some scripts to find the size of each filesystem subtree rooted at a directory. These scripts have the limitation that the loop never quits and to exit the batch file you need to use Ctrl-C and then type ECHO ON to get your DOS prompt back. I have modified the script to get past this limitation. Here's my code. Enjoy.

Thanks,
Bhat


::--------------------------------------------------------
:: Recursion
:: Invokes the 'dirsize' subroutine
::--------------------------------------------------------
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
%%"%WORKING_DIRECTORY%"\dirsize "%%a" 2>NUL
call:dirsize "%%a"
cd %WORKING_DIRECTORY%
)


::--------------------------------------------------------
:: 'dirsize' Subroutine starts here
::--------------------------------------------------------
:dirsize
@echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI
set /a sum=!sum!+!value!
)
@echo %CD% !sum!

bha...@lycos.com

unread,
Jan 18, 2014, 2:08:30 PM1/18/14
to
On Saturday, January 18, 2014 10:33:35 AM UTC-6, bha...@lycos.com wrote:
> Greetings,
>
>
>
> http://www.bish.co.uk/forum/index.php?topic=58.0 provides some scripts to
> find the size of each filesystem subtree rooted at a directory. These scripts
> have the limitation that the loop never quits and to exit the batch file you
> need to use Ctrl-C and then type ECHO ON to get your DOS prompt back. I have
> modified the script to get past this limitation. Here's my code. Enjoy.

v0.1 below:

::--------------------------------------------------------
:: Recursively find the size of each filesystem subtree
:: rooted at a directory
::
:: Modified from http://www.bish.co.uk/forum/index.php?topic=58.0
::
::--------------------------------------------------------


::--------------------------------------------------------
:: Recursion
:: Invokes the 'dirsize' subroutine
::--------------------------------------------------------
echo off
set WORKING_DIRECTORY="%cd%"
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd "%%a"
call:dirsize
Reply all
Reply to author
Forward
0 new messages