Link to home
Start Free TrialLog in
Avatar of FirstCoastYMCA
FirstCoastYMCA

asked on

How to search for a string out of a text file with VBScript

Hey all,

I am using the following VBScript to get the computer's IP address and spit it out to a text file:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("%comspec% /c ipconfig >> c:\youripconfig.txt")

I now want another VBScript to search the text file for this line:


IP Address. . . . . . . . . . . . : 172.29.212.153

and convert the 3rd octet (the 212) to an int value that I can perform IF <> functions on.

any ideas??

Avatar of benjami
benjami
Flag of Andorra image

Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")
objName = objFSO.GetTempName
objTempFile = objName
objShell.Run "%comspec% /c ipconfig >" & objTempFile, 0, True
Set objTextFile = objFSO.OpenTextFile(objTempFile, 1)
Do While objTextFile.AtEndOfStream <> True
    strtext = objTextFile.ReadLine
    If InStr(strtext, "IP Address") > 0 Then
        Wscript.Echo Split(strtext, ":")(1)
        Exit Do
    End If
Loop
objTextFile.Close
objFSO.DeleteFile (objTempFile)
ASKER CERTIFIED SOLUTION
Avatar of lidingo
lidingo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for sharing lidingo, but I don't belive that parsing the ipconfig output is a great way to achive this because:

1. It's very cumbersome
2. What if there are more than one network card attached? You'll never get to know other than the first. On the other hand, you'll find all NC in the registry.
3. What if you run the code on a non-english system? instr("IP") won't work, because on my system the word "IP" appears right in the header of the output.  InStr(strtext, "IP Address") also won't work because "Address" is translated.
Avatar of FirstCoastYMCA
FirstCoastYMCA

ASKER

Well I tried lidingo's script and it bugs out on me. Did this work for you lidingo? Any other ideas?
How come you accepted the answer before trying?
hi.

It worked fine for me.
(Also i can agree that parsing a translated output can cause problems is you use it on different computers with different languages, but most of the time you have the same language, otherwise its simple enough to change it. Also the post was to parse the textfile so i just did what the post asked me for :) Works for me on a swedish system. Thats why i just used IP instead of IP Address. )

What was the error message?

Just a stupid question.
But are you sure that the file "c:\youripconfig.txt" exists?
The program wont work if the file dont exist...
as a bonus i will send another sample for reading the registry.
If you find out where in the registry the ip is it will be easy enough to change the program above.

------------------------

Dim WSHShell,strRegKey
Set WSHShell = WScript.CreateObject("WScript.Shell")
strRegKey="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Productid"
WScript.Echo "Win Productid: " & WSHShell.RegRead(strRegKey)


/lidingo