Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying the host file

Status
Not open for further replies.

cdonkin

IS-IT--Management
May 22, 2009
6
US
Hello,

I want to scan the hosts file for a specific entry and then report back to me if it is there. If it isn't i want it to be entered. The following script works. If the entry is not located it enters it. If the entry is located it reports back.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts", ForReading)
Set objFileWrite = objFSO.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts",ForAppending)

Set oRegEx = CreateObject("VBScript.RegExp")

oRegEx.Pattern = "^129\.111\.111\.20\tlinks\.link\.co\.uk"


oRegEx.Global = True

Const ForReading = 1
Const ForAppending = 8

Dim i
i=0
Do Until objFile.AtEndOfStream
   strSearchString = objFile.ReadLine
   Set aMatches = oRegEx.Execute(strSearchString)  
   If aMatches.Count > 0 Then
       For Each strMatch in aMatches   
           Wscript.Echo strSearchString
           i=2 
       Next
  End IF    
Loop

If i < 1 Then
   objFileWrite.WriteLine("129.111.111.20    links.link.co.uk    links")
   WScript.Echo("Writing")
End If

However when i add :


Code:
Set bRegEx = CreateObject("VBScript.RegExp")

bRegEx.Pattern = "^129\.111\.111\.20\tblink\.blink\.co\.uk"

And a loop:

Code:
Dim a
a=0
Do Until objFile.AtEndOfStream
   strSearchString = objFile.ReadLine
   Set aMatches = bRegEx.Execute(strSearchString)  
   If aMatches.Count > 0 Then
       For Each strMatch in aMatches   
           Wscript.Echo strSearchString
           i=2 
       Next
  End IF    
Loop

and an if statement :

Code:
If i < 1 Then
   objFileWrite.WriteLine("129.111.111.20    blink.blink.co.uk   blinks")
   WScript.Echo("Writing")
End If

The script now will find the first entry if it is there, if it isn't it will enter it. However it never finds the second pattern and always adds it even if it is there.

If i comment out all the code for the oRegEx then the bRegEx pattern works. It finds the entry or enters it if it cannot find it. So why does it not work with both in ?
 
Please post the "total" script so we can see where you are adding the sections to make troubleshooting easier.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts", ForReading)
Set objFileWrite = objFSO.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts",ForAppending)

Set oRegEx = CreateObject("VBScript.RegExp")
Set aRegEx = CreateObject("VBScript.RegExp")


oRegEx.Pattern = "^149\.24\.223\.10\tbannerbaseuk\.myudc\.co\.uk"
aRegEx.Pattern = "^149\.24\.223\.11\tlumbaseuk\.myudc\.co\.uk"

oRegEx.Global = True
aRegEx.Global = True



Const ForReading = 1
Const ForAppending = 8

Dim i
Dim a

i=0

Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
   Set aMatches = oRegEx.Execute(strSearchString)  
    If aMatches.Count > 0 Then
        For Each strMatch in aMatches   
        	Wscript.Echo strSearchString
            i= 2               
        Next
     End IF  
Loop

If i < 1 Then
	objFileWrite.WriteLine("149.24.223.10	bannerbaseuk.myudc.co.uk	bannerbaseuk")
	WScript.Echo("Writing")
End If

a=0
Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set bMatches = aRegEx.Execute(strSearchString)  
    If bMatches.Count > 0 Then
      For Each strMatch in bMatches   
      	Wscript.Echo strSearchString
            a=2        
       Next
   End IF    
Loop

If a < 1 Then
	objFileWrite.WriteLine("149.24.223.11	lumbaseuk.myudc.co.uk	lumbaseuk")
	WScript.Echo("Writing")
End If
 
You are checking for
aRegEx.Pattern = "^149\.24\.223\.11\[red\t[/red]lumbaseuk\.myudc\.co\.uk"

but are adding
("149.24.223.11 lumbaseuk.myudc.co.uk lumbaseuk")

So it will walwas add it in (note the extra character in red).

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
The second loop never executes because you're already at EOF.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
reposting my reply to correct the TGML

You are checking for
aRegEx.Pattern = "^149\.24\.223\.11\[b\[red]t[/red]lumbaseuk[/b]\.myudc\.co\.uk"

but are adding
("149.24.223.11 lumbaseuk.myudc.co.uk lumbaseuk")

So it will always add it in (note the extra character in red).

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks for the input but the character in red is \t which is a tabbed space in regular expressions.

PHV - How would i get the second loop to run?
 
How would i get the second loop to run
Close and Open the file before the second loop.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top