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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract Value From Text File

Status
Not open for further replies.

Chr0nic

MIS
Jun 14, 2002
13
0
0
US
I have a text file which takes the following format:

DHCP Server version 5.5
Discovers = 514.
Offers = 162.
Requests = 196.
Acks = 1249.
Naks = 4.
Declines = 0.
Releases = 1.
ServerStartTime = 01/20/2003 09:40:28.
Scopes = 1.
Subnet = 192.168.102.0.
NumAddressesInuse = 130.
NumAddressesFree = 9.
NumPendingOffers = 0.
Command successfully completed.

I want to extract the value of NumAddressesFree using VBScript in order to generate alerts when servers run low on DHCP addresses. Any pointers or help would be greatly appreciated.

Regards,

Carl
 
use FileSystem and FileStream objects, ReadLine and Split methods. Water is not bad as long as it stays out human body ;-)
 
read each line in the file and put it through an instr (in string) check

e.g.

if (instr (linefromfile, "NumAddressesFree",1)) > 0 then
split string on equals
read right hand side

===============
Security Forums
 
Thanks for the pointers guys, I'm a bit of a beginner at VBScript so any and all help is greatly appreciated!!
 
here's a tip get the vbscript 5.5 chm help file from microsoft. read it. i dont mean learn it. but knowing what's possible, even if u dont know all the syntax, goes a long way when you're thinking how to accomplish tasks :) ===============
Security Forums
 
Thanks for the tips, here's the code I finally came up with, it's probably not the tidiest way of doing it but it works!!!

Const ForReading = 1
Dim fso, MyFile, Text, Fail
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso_OpenTextFile("D:\DHCPTesting.txt", ForReading)
Done = False
While Not Done
If MyFile.Line <> 13 Then
MyFile.SkipLine
Else Done = True
End If
Wend
Text = MyFile.ReadLine
Text = Right(Text, 4)
Text = Left(Text, 3)
If Text < 123 Then
Fail = True
Else Fail = False
End If
wscript.Echo Fail

Any comments greatly appreciated
 
i think this is a more failproof way of getting what u want.

Const ForReading = 1
Dim fso, MyFile, Text, Fail
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set MyFile = fso_OpenTextFile(&quot;D:\DHCPTesting.txt&quot;, ForReading)

Do While myFile.AtEndOfStream <> True

teststr = myfile.readline
if instr(teststr, &quot;NumAddressesFree&quot;) > 0 then exit do

loop
myfile.close
linewewant = split(teststr,&quot;=&quot;)
numaddressesfree = linewewant(1)
===============
Security Forums
 
Thanks for that, I was struggling with the instr function which is why I went the way I did!!!! Yours is far more efficient and failproof. I'm getting there slowly with it though I think, the more I do the more seems to fall in to place!!! Thanks again.
 
have you got the ms vbscript helpfile?
it's exceptionally handy.
knowing functions that are available even if you dont exactly know how to use them goes a long way.
i dont do vbscript often enough to remember exactly what syntax to use, so i'm always having to refer to the help file.

if u havent got it it's here:
===============
Security Forums
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top