MS-DOS

Moderators: blip
Number of threads: 362
Number of posts: 872

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Dos Command/Batch file to find a folder size Posted by trusp on 5 Dec 2008 at 5:01 AM
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
Report
Re: Dos Command/Batch file to find a folder size Posted by xanque on 10 Dec 2008 at 11:16 PM
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
Report
Re: Dos Command/Batch file to find a folder size Posted by trusp on 10 Dec 2008 at 11:30 PM
Xanque,

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

Trusp
Report
Re: Dos Command/Batch file to find a folder size Posted by nashenaas1 on 24 Feb 2013 at 10:15 PM
Hi all.
I want to change this cod to work in loop mode and list name & size & filecountall of each subfolders.
But in output of new code; just "folder name" is changed and "foldersize" & "foldercount" for all subfolder is the same.
Please correct this code and help me.
Thanks.
@echo off
for /d %%D in (".\*") do (
echo "%%D"
For /F "tokens=*" %%a IN ('"dir /s /-c /a "%%D" | find "bytes" | find /v "free""') do Set xsummary=%%a
For /f "tokens=1,2 delims=)" %%a in ("%xsummary%") do set xfiles=%%a&set xsize=%%a
Set xsize=%xsize:bytes = %
Set xsize=%xsize: = %
echo %xsize%
echo %xfiles%
)
Report
Re: Dos Command/Batch file to find a folder size Posted by cewittic on 22 Jan 2010 at 3:42 PM
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.
Report
Re: Dos Command/Batch file to find a folder size Posted by JMarchesoni on 31 May 2010 at 1:37 PM
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...
Report
Re: Dos Command/Batch file to find a folder size Posted by xanque on 4 Mar 2011 at 6:47 PM
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
Report
Re: Dos Command/Batch file to find a folder size Posted by u2plod on 3 Dec 2011 at 6:12 PM
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 Files\Adobe
...
99999M C:\Windows
99999M C:\Windows\winsxs
...

I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
Report
Re: Dos Command/Batch file to find a folder size Posted by u2plod on 3 Dec 2011 at 6:14 PM
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 Files\Adobe
...
99999M C:\Windows
99999M C:\Windows\winsxs
...

I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
Report
Re: Dos Command/Batch file to find a folder size Posted by u2plod on 3 Dec 2011 at 6:44 PM
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 Files\Adobe
...
99999M C:\Windows
99999M C:\Windows\winsxs
...

I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
Report
Re: Dos Command/Batch file to find a folder size Posted by u2plod on 3 Dec 2011 at 6:50 PM
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 Files\Adobe
...
99999M C:\Windows
99999M C:\Windows\winsxs
...

I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
Report
Re: Dos Command/Batch file to find a folder size Posted by u2plod on 3 Dec 2011 at 7:20 PM
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 Files\Adobe
...
99999M C:\Windows
99999M C:\Windows\winsxs
...

I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
Report
Re: Dos Command/Batch file to find a folder size Posted by u2plod on 3 Dec 2011 at 7:45 PM
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 Files\Adobe
...
99999M C:\Windows
99999M C:\Windows\winsxs
...

I will then manipulate this output in the way I want. Are you able to devise a script that can produce the above output?
Report
Re: Dos Command/Batch file to find a folder size Posted by cewittic on 22 Jan 2010 at 3:45 PM
broken forum...duplicate post
Report
Re: Dos Command/Batch file to find a folder size Posted by allancass on 13 Mar 2010 at 4:49 PM
Try this freeware
http://www.mindgems.com/products/Folder-Size/Folder-Size.html
it is really good and contains no adware or spam!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.