Search code examples
windowsbatch-filecommand-linedoscommand-prompt

How to set texts in a hosts file (found in C:/windows/system32/drivers/etc folder) using command prompt or a batch file?


I want my system to automatically type certain statements in the host file in a sheduled time. To do that I need to execute a batch file which contains the required commands for setting the statements in host file. What are the syntax required to set statements in a file, specifically a hosts file or a text file.


Solution

  • This will add a couple of entries to the HOSTS file, when run as administrator:

    @echo off
    attrib -r -a -s -h C:\WINDOWS\System32\Drivers\etc\hosts 1>nul 
    C:\WINDOWS\System32\Drivers\etc\hosts >> echo 200.100.10.20    www.bogus.com
    C:\WINDOWS\System32\Drivers\etc\hosts >> echo 200.100.10.22    www.fake.com