Search code examples
batch-filerecursiondos

recurse directory for file extension with total?


I want to know how much disk space is being used by zip files (or any other extension) in a particular directory tree. Is this possible on the command line (in a batch program)?

I can list them, ie: dir /s *.zip or using "forfiles": forfiles /p d:\wincap /s /m *.zip /c "cmd /c echo @fsize"

I need it in a batch program, because I want to run it on 100+ servers (w2k3). I am accustomed to unix/linux, and dos is giving me a headache ;)

Ideas?

Thanks! Ron


Solution

  • Here's a modified version of the one found: http://en.kioskea.net/forum/affich-42442-dos-command-batch-file-to-find-a-folder-size

    @echo off
    setLocal EnableDelayedExpansion
    set /a value=0
    set /a sum=0 
    FOR /R %1 %%I IN (%2) DO (
    set /a value=%%~zI/1024/1024
    set /a sum=!sum!+!value!
    )
    @echo Size is: !sum!  MB
    

    In order to avoid overflows it converts values to MB right away so it will round and thus under-report the total but hopefully this puts you on the right track at least. Save it as dirsize.bat and call it with the directory as the first parameter and the pattern to match as the second:

    dirsize c:\ *.zip