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. . . . . . . . . . . . :

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

any ideas??

Who is Participating?
lidingoConnect With a Mentor Commented:
Since you havent accepted any answers i will post too :)
My is a bit more code but will work for more languages (the most starts with IP but spell address different)

And in the end it does a check on the 3rd octet as you wished



Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile("c:\youripconfig.txt", OPEN_FILE_FOR_READING)

' read everything in an array
inputData = Split(objInputFile.ReadAll, vbNewline)

For each strData In inputData
    strData = replace(strData, " ", "")
    ok1 = instr(strData, ".")
    if(ok1 > 0) then      
      tmp = Split(strData, ".")
      if left(tmp(0), 2) = "IP" then
          tmp2 = Split(strData, ":")

          'IP FOUND!

          my_ip = tmp2(1)
          ip_parts = Split(my_ip, ".")
          WScript.Echo ip_parts(2)

          x = Int(ip_parts(2))

          if x < 50 then
              WScript.Echo x & " is < 50"
              WScript.Echo x & " is => 50"
          end if

      end if
    end if

Set objFileSystem = Nothing

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
objFSO.DeleteFile (objTempFile)
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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.
FirstCoastYMCAAuthor Commented:
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?

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")
WScript.Echo "Win Productid: " & WSHShell.RegRead(strRegKey)

All Courses

From novice to tech pro — start learning today.