Solved

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

Posted on 2006-10-20
9
7,170 Views
Last Modified: 2012-05-05
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
Comment
Question by:FirstCoastYMCA
9 Comments
 
LVL 3

Expert Comment

by:benjami
Comment Utility
0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
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
 
LVL 1

Accepted Solution

by:
lidingo earned 250 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:benjami
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:FirstCoastYMCA
Comment Utility
Well I tried lidingo's script and it bugs out on me. Did this work for you lidingo? Any other ideas?
0
 
LVL 3

Expert Comment

by:benjami
Comment Utility
How come you accepted the answer before trying?
0
 
LVL 1

Expert Comment

by:lidingo
Comment Utility
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
 
LVL 1

Expert Comment

by:lidingo
Comment Utility
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
 
LVL 1

Expert Comment

by:lidingo
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now