Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert string to number

Status
Not open for further replies.

Mich

IS-IT--Management
Dec 26, 2000
452
US
I'm sure this has been answered a million times on a million sites, but for whatever reason I can't find what I'm looking for. Here's my code,

Code:
Set iFSO = CreateObject("Scripting.FilesyStemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
InputFile="\\bhmopfs3\sys\support\Diskspace\serverlist.txt"
Outputfile="\\bhmopfs3\sys\support\diskspace\Freespacelist_" + cstr(Month(now()))+"_"+cstr(day(now()))+".csv"

Set ofile = ofso.createTextFile(OutputFile, True)
Set ifile = iFSO.OpenTextFile(inputfile)  

Const MBCONVERSION= 1048576  
ofile.writeline "Computer,Drive,Disk Size (MB),FreeSpace (MB),% Free"

Do until ifile.AtEndOfLine
   computer	= ifile.ReadLine

   Set objWMIService = GetObject("winmgmts://" & Computer)  
   Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk")


   For Each objLogicalDisk In colLogicalDisk  
      if objLogicalDisk.drivetype=3 Then
         ofile.writeline Computer & "," & objLogicalDisk.DeviceID &_
         "," &  objLogicalDisk.size/MBCONVERSION & "," &_
         objLogicalDisk.freespace/MBCONVERSION & "," &_
         FormatNumber((objLogicalDisk.freespace/MBCONVERSION)/(objLogicalDisk.size/MBCONVERSION), 3)
      end If
   Next  
Loop

MsgBox("Disk space report generated.")

ofile.Close
ifile.Close
Set Input = iFSO.Opentextfile(Outputfile, "1", True)


Do While Input.AtEndOfStream <> True
	arrServerInfo = Input.Readline
	if not isempty(arrServerInfo) Then
	   arrServerDetail = Split(arrServerInfo, ",")
	   ServerName = arrServerDetail(0)
	   ServerDrive = arrServerDetail(1)
	   ServerTotalSpace = arrServerDetail(2)
	   ServerFreeSpace = arrServerDetail(3)
	   PercentFree = arrServerDetail(4)
	   If PercentFree > "10" Then
          MsgBox("Drive " & ServerDrive & " on server " & ServerName & " is low on space.")
	   End If
	End If
Loop

This scripts works great except for the last part. I want message boxes to pop up if a server has less than 10% free space. Problem is, in my output file % free is a string. How do I convert that to a number so that I can use '< 10' to check free space? I've tried CSng, CDbl, etc with no luck.

Thanks in advance.

-If it ain't broke, break it and make it better.
 
Have you tried
Code:
if cint(PercentFree) < 10 then
   MsgBox ...
end if
 
That gives me a type mismatch as does csng, cdbl, etc.

-If it ain't broke, break it and make it better.
 
The problem is the "%". Convert only the numeric portion of the string and you should be fine.

Code:
PercentFree = "9%"
if cint(Left(PercentFree,InStr(PercentFree,"%")-1)) < 10 then
    MsgBox PercentFree
end if
 
Skie, that wasn't the problem, but you pointing that out opened my eyes.

If you'll look on about line 10 I write out colum headers. Well, the type mismatch I've been getting is because the first element I'm trying to convert to numeric is "% Free". Skipping the first line of the text file allows me to process the information correctly.

Sometimes the solution is so simple, yet so hard to find.

-If it ain't broke, break it and make it better.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top