Delete lines from Hosts file

(imported topic written by Mandilaras)

Hello,

I’ve been searching this forum, but didn’t manage to get a clear idea on how to accomplish this:

We need a fixlet that will look for the existence of the hosts file in a Windows XP enviroment, and if it is there, it will remove all the lines from that file that contain a specific string.

Those lines might not be at the same location on all the computers, and that string might appear in that file more than 1 time.

Can anyone provide some assistance on that?

Best regards,

Michail

(imported comment written by BrianPGreen)

I think the relevance for such a fixlet would be something like:

exist line whose ( it contains “foo.example.com” or it contains “bar.example.com” ) of file “C:\Windows\System32\drivers\etc\hosts”

I think for removing the lines, it’d probably be best to write a batch script that can do it and embed the batch script in the action using “createfile until”.

(imported comment written by Rafael Rodriguez)

I use a vbs to introduce lines.

On Error Resume Next

'#####################################################################################################

'# FUNCION: ESCRIBE EN C:\WINDOWS\SYSTEM32\DRIVER\ETC\HOSTS

'#

'# REALIZADO: RAFAEL RODRIGUEZ AND RUBEN ESPINOSA

'#

'# VERSION: 1.2

'#

'#

'#####################################################################################################

Set fso = CreateObject(“Scripting.FileSystemObject”)

Set WshNetwork = WScript.CreateObject(“WScript.Network”)

Set WshArguments = WScript.Arguments

Set WshShell = WScript.CreateObject(“WScript.Shell”)

server = WshShell.ExpandEnvironmentStrings("%SERVIDOR%")

host = “C:\WINDOWS\system32\drivers\etc\hosts”

config = server&"\etc\hosts\hosts"

contenido =""

filtro = “”

salida=""

'####################################################

’ MI FICHERO

'####################################################

If fso.FileExists (config) Then

    Set f1 = fso.OpenTextFile (config,1,False)


    Set f2 = fso.OpenTextFile (host,1,False)


    temp = ""


    contenido = f2.ReadAll


    f2.Close





    Do While f1.AtEndOfStream <> True


            temp =f1.ReadLine


            If InStr (temp,"#") <> 1 Then ' COMPARA LA VARIABLE Y SI LA LINEA NO CONTIENE # THEN


                    'WScript.Echo InStr (1,contenido,temp)





                    If InStr (1,temp, "remplazar") <> 0 Then 'BUSCA LA FUNCION REMPLAZAR





                            pos1 = InStr (1,temp,"10")


                            'MsgBox pos1





                            pos2 = InStr (pos1, temp," ")


                            ip_anterior = Mid (temp,pos1,pos2-pos1)


                            pos3 = InStr (pos2,temp,"10")


                            pos4 = Len (temp)+1


                            ip_despues = Mid (temp,pos3,pos4-pos3)


                            If InStr  (1,contenido,ip_despues) = 0 Then


                                    Set f2 = fso.OpenTextFile (host,2,False)





                                    tt = Replace (contenido,ip_anterior,ip_despues)


                                    f2.Write tt


                                    f2.Close





                             End If





                    End If


                    If InStr (1,temp, "meter") <> 0 Then


                            Set f2 = fso.OpenTextFile (host,8,False)


                            pos5 = InStr (1,temp,"10")


                            pos6 = Len (temp)+1


                            ip_into = Mid (temp,pos5,pos6-pos5)


                            If InStr  (1,contenido,ip_into) = 0 Then        'SI NO EXISTE INTRODUCE CAMPO


                                    WScript.Echo ip_into





                                    f2.WriteBlankLines (1)


                                    f2.Write ip_into


                                    f2.Close


                            End if


                    End If


            End If


    Loop


    f1.Close

End if

(imported comment written by Rafael Rodriguez)

other !

On Error Resume next

Set fso = CreateObject(“Scripting.FileSystemObject”)

Set WshShell = WScript.CreateObject(“WScript.Shell”)

tnsnames=“C:\oracle\product\10.2.0\client_1\NETWORK\ADMIN\tnsnames.ora”

hosts=“C:\Windows\system32\drivers\etc\hosts”

linea=""

'Si ya hay testigo, salimos de la ejecución

If fso.FileExists (“c:\xx.txt”) Then

     WScript.Quit

Else

    'Modificamos el fichero tnsnames.ora



    If fso.FileExists (tnsnames) Then



            Set fr2 = fso.OpenTextFile(tnsnames,1)



            Set fw1 = fso.CreateTextFile(tnsnames + ".new")



            Do While fr2.AtEndOfStream <> True



                    linea = fr2.ReadLine



                    If InStr (1,linea,"192.168.1.1") <> 0 Then



                            fw1.writeline (Replace(linea,"

192.168.1.1
",“host.es”))

                    Else



                            fw1.WriteLine (linea)



                    End If



            Loop



            fr2.Close



            fw1.Close



            fso.MoveFile tnsnames, tnsnames + ".old"



            fso.MoveFile tnsnames + ".new", tnsnames



      End If






    'Modificamos el fichero hosts



    If fso.FileExists (hosts) Then



            Set fr3 = fso.OpenTextFile(hosts,1)



            Set fw2 = fso.CreateTextFile(hosts + ".new")


           



            Do While fr3.AtEndOfStream <> True



                    linea = fr3.ReadLine


                   



                    If InStr (1,linea,"

192.168.1.1
") <> 0 Then

                            fw2.WriteLine ("#" + linea)



                    Else



                            fw2.WriteLine (linea)



                    End If



            Loop



            fr3.Close



            fw2.Close



            'Renombramos los ficheros dejando backup del original



            fso.MoveFile hosts,hosts + ".old"



            fso.MoveFile hosts + ".new",hosts



    End if






    'Dejamos testigo de que la tarea ya se ha realizado                                



    Set fw3 = fso.CreateTextFile ("c:\xx.txt")



    fw3.WriteLine "tnsnames.ora modificado"



    fw3.WriteLine "hosts modificado"



    fw3.Close

End If