Question

Discussion in 'Scripting' started by sanjose, Oct 27, 2019.

  1. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    How can I delete (remove) lines from the hosts files with batch file?

    Hello all,
    I wonder if my question is right place or wrong place. My question is that
    Is there anyway to delete (remove) two specific lines from the hosts file with a batch file like this one that I add lines below ?
    Thank you in advance.
    Here's the one I add
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    10,378
    41,542
    340
    #2 abbodi1406, Oct 27, 2019
    Last edited: Oct 27, 2019
    Not very professional, but works for few lines
    Code:
    findstr /i /v /c:"127.0.0.1 facebook.com" %WINDIR%\system32\drivers\etc\hosts | findstr /i /v /c:"127.0.0.1 youtube.com" > %WINDIR%\system32\drivers\etc\hosts.tmp
    del %WINDIR%\system32\drivers\etc\hosts
    ren %WINDIR%\system32\drivers\etc\hosts.tmp hosts
     
  3. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    @abbodi1406,
    Thank you so much for your help. One more question please. If I need to remove two more lines from hosts file (totally 4 lines), Can you show me how to do that please.
     
  4. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    10,378
    41,542
    340
    repeat the commands changing the two URLs
     
  5. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    #5 sanjose, Oct 27, 2019
    Last edited: Oct 27, 2019
    (OP)
    @abbodi1406,
    I just saved it as .bat and run it as admin. Unfortunately, it does not remove two specific lines " facebook & youtube" and a "hosts.tmp" file was saved . Please check your command lines. Thank you again.
     
  6. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    10,378
    41,542
    340
    @sanjose

    fixed, (del command before ren)
     
  7. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    @abbodi1406,
    It does not work. Can you check again please. " 127.0.0.1 facebook.com & 127.0.0.1 youtube.com " can not be removed from hosts file. Thanks for your help and Have a great day.