Solved

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

Posted on 2010-09-05
7
1,790 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 68

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 52

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now