I have a function that reads a text file containing two lines, it reads the second line and tries to convert the string to a Long integer. My problem is that it's not converting the string, the code for the function is as follows:
Function getLastUsed (txtFilePath)
'Read timestamp created by full backup script
Const ForReading = 1
'Error Handling
On Error Resume Next
Err.Clear
Set objFile = Fso.OpenTextFile(txtFilePath, ForReading)
'Read if file was successfully opened, else return 0
If Err.Number = 0 Then
'Value will be on the second line, so readLine twice
Dim valString
valString = objFile.Readline
valString = objFile.Readline
objFile.Close
'If not empty continue, else return 0
If Not IsEmpty(valString) Then
'Return value is IsNumeric, else return 0
If IsNumeric(valString) Then
Dim valInt
valInt = CLng(valString)'<--Won't convert, variable valInt is empty after this line
getLastUsed = valInt
Else
getLastUsed = 0
End If
Else
getLastUsed = 0
End If
Else
getLastUsed = 0
End If
End Function
I'm using Visual Studio to step through and debug, looking at the 'valInt' variable, it is empty after I call CLng or CInt. However, if I hardcode a value, like valInt = CLng("18") it works fine. I'm not sure what I'm missing, any help is appreciated.
UPDATE
Ok so I tried running the script again, this time replacing the value in the text file, which was 20110511123500 to something shorter 2011 and it worked. So it seems like it was a precision issue. That leads to a second question, what's the largest number VBScript can handle as far as conversion is concerned? The numeric value indicated is the amount of disk space on a hard drive, so I need to be able to read large numbers. Thanks for the hint Pepto.
UPDATE 2
Using double did the trick, thanks G Mastros.