Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-10-20
9
7,174 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
ID: 17775106
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 17775157
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
ID: 17779898
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:benjami
ID: 17780434
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
 

Author Comment

by:FirstCoastYMCA
ID: 17813399
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
ID: 17814232
How come you accepted the answer before trying?
0
 
LVL 1

Expert Comment

by:lidingo
ID: 17860211
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
ID: 17860505
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
ID: 17860589
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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