Simply Math..Huge headache

Microsoft Windows xp professional w/serv...
March 10, 2010 at 11:44:24
Specs: Windows XP, 2g
I am using the following code to check the size of a directory from the command prompt.
It works ok but the sizes are not exact and it always displays in kilobytes(i'd like it to display in megabytes and gigabytes as thats what we mostly have. This is what I have put together so far. I know its somthing simply mabey a mathmatical error but for some reason, not working correctly.

Any thoughts?

@echo off
setLocal EnableDelayedExpansion
set /a sum=0
set /a value=0
set /a KiloBytes=0
set /a MegaBytes=0
set /a cMegaBytes=0
set /a GigaBytes=0
set /a cGigaBytes=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI

set /a sum=!sum!+!value!
if !sum! gtr 1024000 set /a cMegaBytes=!sum!\1024000 | set /a MegaBytes=!MegaBytes! + cMegaBytes | set /a sum=!sum!-!cMegaBytes! | set /a cMegaBytes=0

@echo Size is: !sum! Kilobytes
@echo Size is: !MegaBytes! Megabytes
@echo Size is: !GigaBytes! Gigabytes

See More: Simply Math..Huge headache

Report •

March 10, 2010 at 12:26:56
Okay, first glance:
1) FOR will tell you the size in bytes, not kilobytes.
2) 2^20 is not 1024000, it is 1,048,576
3) SET does not have a '\' operator
4) SET /A won't save any variable if it's part of a pipe
5) You're trying to subtract the total number of megabytes from the current byte counter, instead of subtracting the total number of bytes in a megabyte
6) SET uses signed 32-bit integers, so expect the script to break if the total byte count should ever go above 2^31-1 (2GB or up, basically). This can be incredibly easy to do, because you only ever subtract 1MB per pass of the loop.

Perhaps you should just use a third party program? Like WinDirInfo?

Report •
Related Solutions

Ask Question