FirstCoastYMCA
asked on
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.Shel l")
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??
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.Shel
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??
try http://www.codeproject.com/vbscript/ipaddress.asp
Set objFSO = Wscript.CreateObject("Scri pting.File SystemObje ct")
Set objShell = Wscript.CreateObject("Wscr ipt.Shell" )
objName = objFSO.GetTempName
objTempFile = objName
objShell.Run "%comspec% /c ipconfig >" & objTempFile, 0, True
Set objTextFile = objFSO.OpenTextFile(objTem pFile, 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)
Set objShell = Wscript.CreateObject("Wscr
objName = objFSO.GetTempName
objTempFile = objName
objShell.Run "%comspec% /c ipconfig >" & objTempFile, 0, True
Set objTextFile = objFSO.OpenTextFile(objTem
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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?
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?
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...
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("WScr ipt.Shell" )
strRegKey="HKLM\SOFTWARE\M icrosoft\W indows\Cur rentVersio n\Producti d"
WScript.Echo "Win Productid: " & WSHShell.RegRead(strRegKey )
/lidingo
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("WScr
strRegKey="HKLM\SOFTWARE\M
WScript.Echo "Win Productid: " & WSHShell.RegRead(strRegKey
/lidingo