[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7180
  • Last Modified:

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??

0
FirstCoastYMCA
Asked:
FirstCoastYMCA
1 Solution
 
EDDYKTCommented:
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)
0
 
lidingoCommented:
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


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

Const OPEN_FILE_FOR_READING = 1

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"
          else
              WScript.Echo x & " is => 50"
          end if

      end if
    end if
Next

objInputFile.Close
Set objFileSystem = Nothing


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
benjamiCommented:
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.
0
 
FirstCoastYMCAAuthor Commented:
Well I tried lidingo's script and it bugs out on me. Did this work for you lidingo? Any other ideas?
0
 
benjamiCommented:
How come you accepted the answer before trying?
0
 
lidingoCommented:
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?

0
 
lidingoCommented:
Just a stupid question.
But are you sure that the file "c:\youripconfig.txt" exists?
The program wont work if the file dont exist...
0
 
lidingoCommented:
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now