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!

Remove entries

Status
Not open for further replies.

GrimR

IS-IT--Management
Jun 17, 2007
1,149
ZA
Is is possible to modify this script to delete entries in the hosts file?

Code:
 strSrcFile = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\System32\Drivers\Etc\Hosts"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
arrHosts = Array(_
	"192.168.0.1  	server.com", _
	"196.213.0.10 ", _
	"196.213.0.10 ", _
	"192.168.0.2	server.com", _
	"192.168.0.3	server.com", _
	"192.168.0.4	server.com", _
	"192.168.0.5	server.com", _
	"192.168.0.6	server.com" _
	)
 
If objFSO.FileExists(strSrcFile) = True Then
	Set objFile = objFSO.OpenTextFile(strSrcFile, 1, False)
	strContents = objFile.ReadAll
	objFile.Close
	Set objFile = Nothing
	arrContents = Split(strContents, VbCrLf)
	For Each strHost In arrHosts
		strIPAddress = Split(strHost, vbTab)(0)
		strDNSName = Split(strHost, vbTab)(1)
		boolHostFound = False
		For intLine = LBound(arrContents) To UBound(arrContents)
			If InStr(arrContents(intLine), strIPAddress) > 1 Or InStr(arrContents(intLine), strDNSName) > 1 Then
				boolHostFound = True
				arrContents(intLine) = strIPAddress & vbTab & strDNSName
			End If
		Next
		If boolHostFound = False Then
			ReDim Preserve arrContents(UBound(arrContents) + 1)
			arrContents(UBound(arrContents)) = strIPAddress & vbTab & strDNSName
		End If
	Next
	Set objFile = objFSO.CreateTextFile(strSrcFile, True)
	objFile.Write Join(arrContents, VbCrLf)
	objFile.Close
	Set objFile = Nothing
	MsgBox "File modified."
Else
	MsgBox "File not found."
End If
 
[1]
>Is is possible to modify this script to delete entries in the hosts file?
Sure you can. The layout would be (a) add an array like arrHosts, say arrHostsToDelete; (b) add a loop similarly constructed like that of for intLine=... next, only this time you make arrContents(inline)="" instead.

[2] The major problem yet is that the script as it stands is not properly made.

[2.1]
>If InStr(arrContents(intLine), strIPAddress) > 1 Or InStr(arrContents(intLine), strDNSName) > 1 Then

If you want check for existence of the component, it should be this instead.

[tt]If InStr(arrContents(intLine), strIPAddress) [red]<> 0[/red] Or InStr(arrContents(intLine), strDNSName) [red]<> 0[/red] Then[/tt]

[2.2] But another major problem is that you should check for leading non-whitespace (whitespace including space and tab) see if it is a comment character (#), if it is why should you do any action. The comment line can say any thing that might contain the targetted ip's. It should be left untouched. If you replace it, it becomes an effective line rather than a comment.

[2.3] To check for leading #, trim() is not sufficient to eliminate leading tab's. You should do more to properly identify it as a comment line. A regexp to trim should be used.
 
Thanks the 1 and 1 thing was me, messing around
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top