Solved

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

Posted on 2006-10-20
9
7,173 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need VBS version of Excel date conversion 4 70
VB error "Type mismatch" 2 53
Exit a vb6 apps when a calling it apps closes 15 46
Problem to With line 4 56
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

813 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

11 Experts available now in Live!

Get 1:1 Help Now