bat file display folder size

March 4, 2009 at 07:41:59
Specs: Windows 2000
Hi
I need a batch file that displays the name and size (in MB) of all folders within a given directory. Preferably outputed to a txt or csv file.

See More: bat file display folder size

Report •


#1
March 4, 2009 at 10:21:19

@echo off > newfile & setLocal EnableDelayedExpansion & @echo off > new.csv

set orig=%CD%

set /p L=path ?
if !L!'==' goto :eof

for /f "tokens=* delims= " %%a in ('dir/b/s/ad !L!') do (
pushd %%a
set /a SZ=0
for /f "tokens=* delims= " %%f in ('dir/b/a-d') do (
set /a SZ+=%%~Zf
set /a MB=!SZ!/1000000
)
call :sub1
)
goto :eof

:sub1

echo !CD! !SZ! bytes >> %orig%\newfile
echo !CD! !MB! Mbytes >> %orig%\newfile
echo ------------------------------------------------------- >> %orig%\newfile

echo !CD!, !SZ!, bytes >> %orig%\new.csv
echo !CD!, !MB!, Mbytes >> %orig%\new.csv

goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
March 5, 2009 at 02:41:04
[Edit - Tweaked script]

I have a much more complicated script - the only real advantage being that it should work on directories larger than 2gb and path names that contain "!". The megabytes are (hopefully) calculated very close to the exact values. Tested on xp - don't know about Win2000.....

@echo off
SETLOCAL
echo "%~1"|find "?"||if exist "%~1\*" if not "%~2"=="" (if /i "%~2"=="/s" goto begin) else goto begin
echo Useage:&echo "%~nx0" "directory name" [/s]
echo In most cases diretory name must be fully specified.
echo If directory name contains either spaces or ampersands
echo it must be enclosed in "double quotes".&echo.
echo the /s switch is optional, if present the output will
echo recurse into subdirectories otherwise it will only
echo record directories below the specified directory.
pause>nul
goto :eof
:begin
pushd "%1\"
echo ^<path^>, ^<rounded size mb^>, ^<size bytes^> 
for /f "delims=" %%a in ('echo "%~1"^&^&dir /b/ad%2 "%~1"') do (
set files=
for /f "tokens=3 delims=	 " %%c in ('2^>nul dir "%%~fa" /s/-c/a^|find /i "file(s)"') do set sv=%%c&&SET BYTES=%%c
if not defined sv (
>&2 echo "%%~fa\" Unsearchable!
) else (
call :div1024
call :div1024
cmd /v:on /c echo "%%~fa\", !sv!, !BYTES!
)
)
popd
goto :eof

:div1024
SETLOCAL ENABLEDELAYEDEXPANSION
set total=
if "%sv:.=%"=="%sv%" set sv=%sv%00
set sv=%sv:.=%
:split
if defined sv (
if !sv! gtr 999 (
set /a ccnt+=1
set svp!ccnt!=%sv:~-4%
set sv=%sv:~0,-4%
goto split
) else (
set /a ccnt+=1
set svp!ccnt!=%sv%
))
:loop
if defined svp%ccnt% (
if "!svp%ccnt%:~0,1!"=="0" (
set svp%ccnt%=!svp%ccnt%:~1!
goto loop
)) else (
set svp%ccnt%=0
goto pad
)
set vcnt=0
:reduce
If !svp%ccnt%! geq 1024 (
If !svp%ccnt%! geq 1024000 (
set /a svp%ccnt%-=1024000
set /a vcnt+=1000
goto reduce
) else (
If !svp%ccnt%! geq 102400 (
set /a svp%ccnt%-=102400
set /a vcnt+=100
goto reduce
) else (
If !svp%ccnt%! geq 10240 (
set /a svp%ccnt%-=10240
set /a vcnt+=10
goto reduce
) else (
set /a svp%ccnt%-=1024
set /a vcnt+=1
goto reduce
))))
:pad
if "!vcnt:~3,1!"=="" set vcnt=0!vcnt!
if "!vcnt:~3,1!"=="" set vcnt=0!vcnt!
if "!vcnt:~3,1!"=="" set vcnt=0!vcnt!
set total=%total%%vcnt%
set vcnt=0
set tmpcnt=%ccnt%
set /a ccnt-=1
if !svp%tmpcnt%! gtr 0 (
set carry=!svp%tmpcnt%!
set svp!ccnt!=!carry!!svp%ccnt%!
)
if %ccnt% equ 0 goto finish
goto loop
:finish
if not defined total set total=0.00&&goto output
if "!total:.=!"=="!total!" set total=!total:~0,-2!.!total:~-2!
if "%total:~0,1%"=="0" set total=%total:~1%&&goto finish
:output
if "!total:~0,1!"=="." set total=0!total!
if defined carry if "!total!"=="0.00" set total=0.01
endlocal&set sv=%total%
goto :eof


Report •

Related Solutions


Ask Question