Solved

Batch File: Invalid number. Numbers are limited to 32-bits of precison.

Posted on 2010-09-05
7
1,847 Views
Last Modified: 2012-05-10
Hi there,

I have this script that will not only output the local drives infos, but also convert units to GB.

My problem is when a drive has more than 1TB, I get the following error:
Invalid number. Numbers are limited to 32-bits of precison.

Thanks for your help,
Rene
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

IF EXIST %~n0.txt DEL %~n0.txt

FOR /F "tokens=1-7 delims=," %%A IN ('@WMIC LogicalDisk Where DriveType^=^"3^" Get DeviceID^,DriveType^,FileSystem^,FreeSpace^,Size^,VolumeSerialNumber /Format:csv ^| find ^"3^"') DO (
	REM ECHO %%A %%B %%C %%D %%E %%F %%G
	SET DRIVE=%%B
	SET FreeSpace=%%E
	SET TotalSpace=%%F
	SET /A FreeSpace=!FreeSpace:~0,-3!/1024*1000/1024/1024
	SET /A TotalSpace=!TotalSpace:~0,-3!/1024*1000/1024/1024
	ECHO !DRIVE! !TotalSpace!GB !FreeSpace!GB>>%~n0.txt
	)
PAUSE
EXIT

Open in new window

0
Comment
Question by:ReneGe
7 Comments
 
LVL 14

Accepted Solution

by:
Psy053 earned 167 total points
ID: 33609465
Have you considered using a VBScript to do it?

strComputer = "."
strFileName = "C:\DiskSpace.log"
Const Gig = 1073741824

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName,8,True)

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk WHERE Drivetype='3'")
Wscript.Echo strComputer
For Each objDisk in colDisks
	objFile.WriteLine objDisk.DeviceID & " " & _
	FormatNumber(objDisk.Size / Gig, 2,,,0)  & " " & _ 
	FormatNumber(objDisk.FreeSpace / Gig, 2,,,0)
Next
objFile.Close

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 33609472
The limit is 2147483647 (2^32-1). As sson as you use set /A, the string in FreeSpace and TotalSpace will be interpreted as integer, and that causes the overflow.
Just take off more of the digits from the end, as you do already with the 3 byte digits. Depending on the accuracy you want to maintain you can just remove another 3 digits (the KB part):

        SET /A FreeSpace=!FreeSpace:~0,-6!/1024*1000/1024
        SET /A TotalSpace=!TotalSpace:~0,-6!/1024*1000/1024

0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 166 total points
ID: 33610522
Search is your friend Rene :-).

A couple of solid related posts here.  FWIW I think your choices are (a) use a third part util for the math that can handle large numbers, (b) use VB script, either for the whole task, or just create a small helper routine where you pass it the number and it returns the formatted version, or (c) you remove some accuracy and live with an approximation in BAT only code.

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_26414453.html

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_24881845.html

~bp
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 10

Author Comment

by:ReneGe
ID: 33612227
==> bp
According to: http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_26414453.html
I have the same problem, except that I get the error starting at 100GB insted of 1TB.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_24881845.html
Provides me with very unaccurate output

==> Qlemo
SET /A FreeSpace=!FreeSpace:~0,-6!/1024*1000/1024
Provides me with very unaccurate output

==> Psy053
The Script does not work.


Thanks guys for helping,
Rene
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33612249
Like bp and Psy053 suggested, I think I'll have to go for VBScript.

Since it would be better that I'm in the VB section, I'll split the points here, close this thread and open a new one.

I'll provide you it's link.

Thanks guys,
Rene
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33612525
0
 
LVL 10

Author Comment

by:ReneGe
ID: 35133739
Hi guys,

I thought you may be interested into this one.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26886544.html

Thanks and cheers,
Rene
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question