Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2074
  • Last Modified:

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

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
ReneGe
Asked:
ReneGe
3 Solutions
 
Psy053Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
Bill PrewCommented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
ReneGeAuthor Commented:
==> 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
 
ReneGeAuthor Commented:
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
 
ReneGeAuthor Commented:
0
 
ReneGeAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now