Discussion:
DOS batch file to recursively find the size of each directory
(too old to reply)
b***@lycos.com
2014-01-18 16:33:35 UTC
Permalink
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!
b***@lycos.com
2014-01-18 19:08:30 UTC
Permalink
Post by b***@lycos.com
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
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!

Loading...