Dos Command/Batch file to find a folder size - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Dos Command/Batch file to find a folder size

trusptrusp Posts: 3Member
Hello,
I need a DOS command/Batch file to get the folder size alone.
Example C:Sample1
Size is :457865

Batch file must be DOS based.

Please help me on this.
Thanks
«1

Comments

  • xanquexanque Posts: 2Member
    Hello,

    I recently had a need to do this also.
    Here is what I came up with.
    It runs a DIR /S on the current directory and returns the second last line, which is the total size.
    EG. 15976 File(s) 2,900,344,439 bytes
    delims=)" splits it down the middle where the ")" character of "file(s)" is.
    The last two SETs take off the word "bytes" and the leading spaces.
    EG. Size is :2900344439
    All the echos except for the last one are for debugging.
    It needs to be in a batch file for the variable names to work.

    @For /F "tokens=*" %%a IN ('"dir /s /-c | find "bytes" | find /v "free""') do @Set summaryout=%%a
    @Echo %summaryout%
    @For /f "tokens=1,2 delims=)" %%a in ("%summaryout%") do @set filesout=%%a&set sizeout=%%b
    @Echo %filesout%
    @Echo %sizeout%
    @Set sizeout=%sizeout:bytes=%
    @Echo %sizeout%
    @Set sizeout=%sizeout: =%
    @Echo Size is :%sizeout%

    Hope it does the trick for you.

    - Xanque
  • trusptrusp Posts: 3Member
    Xanque,

    Thank you very much for your support.
    It worked fine.

    Trusp
  • cewitticcewittic Posts: 2Member
    That other solution is grotesque! For the rest of the world trying to solve the same problem, try this:
    --------
    setlocal enabledelayedexpansion
    set dir=%1
    set size=0

    for /R %dir% %%F in (*) do ( set /a size=!size!+%%~zF )

    echo %size%
    endlocal
    --------
    Some explanation:
    - "for /R %dir%" means loop recursively through the directory dir.
    - %%F is the name of the variable
    - in (*) means "match any filename". You could put something else here, like "in (*.jpg)" if you only wanted to know the total size of the pictures in a directory.
    - set /a size indicates that the value to the right of the = is an arithmetic expression that needs to be evaluated
    - !size! is like %size% but ! indicates you want to use delayed expansion
    - %%~zF gets the size in bytes of the file %%F

    I recommend http://ss64.com/nt/ and http://ss64.com/nt/syntax.html as references for writing batch files.
  • cewitticcewittic Posts: 2Member
    broken forum...duplicate post
  • allancassallancass Posts: 1Member
    Try this freeware
    http://www.mindgems.com/products/Folder-Size/Folder-Size.html
    it is really good and contains no adware or spam!
  • JMarchesoniJMarchesoni Posts: 1Member
    Grotesque or not (I'm going with 'not'), Xanque's solution is much faster than yours, and I for one think it's pretty ingenious. Furthermore, I'm quite sure your solution could have been offered in a much less insulting way...
  • xanquexanque Posts: 2Member
    You are right, cewittic.
    String hack and slash isn't usually my style.
    But I thought, "Why not? When dir /s is doing all the hard work"

    Interestingly, I started with a script that looked very similar to yours, adding totals on the fly in a one-line elegant loop, but inconsistencies, errors and speed led me down the dark path of string manipulation.
    To be honest, I was excited when you offered another option, but alas, it also returned errors on one of my sample sets, and on another sample set the grand total was much, much less than what dir /s returned.
    1)
    Size is :51520954092

    Invalid number. Numbers are limited to 32-bits of precision.
    -1933777592

    2)
    Size is :190833669782

    1802399972

    One says 190GB, the other says 1.8GB. Which would you believe?

    The cleaned-up version, with system and hidden files calculated (/a).
    Something that should have been there in the first place, I know. My original need didn't require it.


    @echo off
    For /F "tokens=*" %%a IN ('"dir /s /-c /a | find "bytes" | find /v "free""') do Set xsummary=%%a
    For /f "tokens=1,2 delims=)" %%a in ("%xsummary%") do set xfiles=%%a&set xsize=%%b
    Set xsize=%xsize:bytes=%
    Set xsize=%xsize: =%
    Echo Size is :%xsize%


    Still ugly?
    It's 100% reliable on any data set.
    That's good enough for me, not sure about the rest of the world.
    I'd encourage them to try both and make up their own mind.

    - Xanque
  • u2plodu2plod Posts: 6Member
    Although I don't understand it, I like your script.

    I have a slightly different need though. I would like to monitor folder sizes over time and need something that can dump recursive folder sizes into a text file, eg.

    99999M C:
    99999M C:Program Files
    99999M C:Program FilesAdobe
    ...
    99999M C:Windows
    99999M C:Windowswinsxs
    ...

    I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
  • u2plodu2plod Posts: 6Member
    Although I don't understand it, I like your script.

    I have a slightly different need though. I would like to monitor folder sizes over time and need something that can dump recursive folder sizes into a text file, eg.

    99999M C:
    99999M C:Program Files
    99999M C:Program FilesAdobe
    ...
    99999M C:Windows
    99999M C:Windowswinsxs
    ...

    I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
  • u2plodu2plod Posts: 6Member
    Although I don't understand it, I like your script.

    I have a slightly different need though. I would like to monitor folder sizes over time and need something that can dump recursive folder sizes into a text file, eg.

    99999M C:
    99999M C:Program Files
    99999M C:Program FilesAdobe
    ...
    99999M C:Windows
    99999M C:Windowswinsxs
    ...

    I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
«1
Sign In or Register to comment.