0

I found about half a dozen (1, 2, 3 etc.) questions about how to add lines to the Windows hosts file but none of them are easy to maintain if you want to frequently update multiple lines with a single batch file in which you can just replace the old lines with your new ones as a block and delete everything else inside the hosts file.

127.0.0.1 example1.com
127.0.0.1 example2.com
127.0.0.1 example3.com
127.0.0.1 example4.com
127.0.0.1 example5.com

I was hoping you guys can tell me what I missed, where to look or maybe even give me an example. I'd really appreciate your help.

Clacers
  • 401
  • 2
  • 6
  • 12

1 Answers1

1

If you are sure you know what you are doing:
to rewrite the whole hosts file with new contents simply:

@Echo off
Set "Hosts=%windir%\System32\drivers\etc\hosts"
Copy "%Hosts%" "%Hosts%.bak"
( Echo 127.0.0.1 example1.com
  Echo 127.0.0.1 example2.com
  Echo 127.0.0.1 example3.com
  Echo 127.0.0.1 example4.com
  Echo 127.0.0.1 example5.com
) > "%Hosts%"

This batch will append to the original hosts file and save a copy with appended date&time hosts_yyyyMMddhhmmss.bak to the current users Documents folder which should be no problem.

@Echo off
for /f "delims=." %%A in (
  'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set "Bak=%USERPROFILE%\Documents\Hosts_%%A.bak"

Set "Hosts=%windir%\System32\drivers\etc\hosts"
Copy /Y "%Hosts%" "%Bak%"

( Echo 127.0.0.1 example1.com
  Echo 127.0.0.1 example2.com
  Echo 127.0.0.1 example3.com
  Echo 127.0.0.1 example4.com
  Echo 127.0.0.1 example5.com
) >> "%Hosts%"
  • Could you post a version of the batch to just add lines without removing existing? :) – Clacers Jul 01 '17 at 10:54
  • Well the second one preserves existing entries but removes comments per your request. Do you want to create entries other than redirections to localhost? –  Jul 01 '17 at 10:56
  • The second script doesn't work. There is no hosts file anymore, only "hosts.new". – Clacers Jul 01 '17 at 11:12
  • The batch works locally but my AntiVirus keeps me from changing the original path what I'm not going to disable for a test. –  Jul 01 '17 at 11:21
  • But why doesn't it work on my system? I use Windows Defender and there was no notification. – Clacers Jul 01 '17 at 11:34
  • To append the entries to the original host file change the first batch to have two redirection symbols in the last line `) >> "%Hosts%"` –  Jul 01 '17 at 11:35
  • Yes, also I needed to remove "Move /Y "%Hosts%" "%Hosts%.bak"" and now it works but I'd much rather keep a backup... – Clacers Jul 01 '17 at 11:45
  • Then choose a different **save** location ;-) –  Jul 01 '17 at 11:46
  • Why doesn't it work like the first one? Why was it necessary to remove the line? :/ – Clacers Jul 01 '17 at 11:51
  • The host file can be used maliciously redirecting known urls to different ones - so editing should IMO be limited/supervised. –  Jul 01 '17 at 12:01
  • I'm sorry but I don't really understand. I want a backup of the hosts file "hosts.bak" like the first script did. The "second" script is just like the first one except with " ) >> "%Hosts%" " as last line so why is it not possible to backup a copy of the hosts file just like the first script did? – Clacers Jul 01 '17 at 12:09
  • See changed second batch. –  Jul 01 '17 at 12:21
  • It works but still no backup. Not in Documents, not in hosts file folder. :( And if it works: So it's really not possible to put the backup in the same directory as the hosts file? – Clacers Jul 01 '17 at 12:33
  • Sorry my fault changed the erroneous move to a copy. –  Jul 01 '17 at 12:37
  • Sorry to say but there is still no backup. :( – Clacers Jul 01 '17 at 12:48
  • Please change the echo to on and run the batch in an open cmd window to see whats happening. Here it works fine (aside from my AV not allowing to change the original hosts file) –  Jul 01 '17 at 12:55
  • It works! It didn't work because my "Documents" folder is in "D:\Documents". After that I've tried change the backup path to "%windir%\System32\drivers\etc\hosts" but that didn't work. I really want to save the backup just as "hosts.bak" inside the hosts folder, no date in the name, no multiple backups. Just one simple "hosts.bak" file. Is that possible? – Clacers Jul 01 '17 at 13:08
  • But you said the original first version doesn't work, try it agagin with echo on to see whats the problem. But you should now have the knowledge to try on your own ;.) –  Jul 01 '17 at 13:12
  • Now I understand. Thank you very very much! I appreciate your time! – Clacers Jul 01 '17 at 13:17
  • One little question: How can I just empty the whole hosts file? I removed all the lines and tried " ) > "%Hosts%" " but nothing changes. – Clacers Jul 01 '17 at 15:10
  • `Type NUL >"%Hosts%"` will reset the file to zero length –  Jul 01 '17 at 15:12
  • Very nice! Thanks! :D – Clacers Jul 01 '17 at 15:20
  • Sorry, another question: Is it possible to "echo" all lines by default so that you can just copy & paste the updated hosts lines without the "echo"s in every line? – Clacers Jul 01 '17 at 15:50
  • If you have new entries in a textfile you can `Type "yourfile.txt" >>"%Hosts%"` But 25 comments are much to much. Put new questions into [new questions](https://stackoverflow.com/questions/ask) –  Jul 01 '17 at 15:57