This is a simple batch (.bat) file to switch between a default (“empty”) hosts file and one that contains your definitions to block ad-sites. The purpose of this is to allow you to “turn off” the hosts file, in order to view content such as advertisements that you otherwise want blocked. This can be necessary as certain sites such as salon.com give you a ‘free’ daypass to view content if you first view advertisements.
You’ll have to close whichever web browser you use, run the batch file, then reopen the application, as browsers ‘cache’ the hosts file upon loading.
First of all, if you don’t use a hosts file, check out the wiki on what they are, and then download one. At the bottom of the wiki are a number of hosts files you can use. Please note that I’m not recommending a hosts file for security, and you may decide as you wish what performance hit it gives. I haven’t ever noticed a performance problem as a result.
First, make sure you have a backup of the original hosts file. It’s just a text document with the definition: 127.0.0.1 localhost
All this does is define the loopback so that if you try to visit http://localhost, it’s directed to http://127.0.0.1, which is your local computer.
So you’ve got the default/original hosts file, and your spiffy ad-blocking hosts file.
Name one both hosts.1 and hosts.2, and the other just hosts; it doesn’t matter which is which, but you’ll want to be able to see the filesizes so you can easily differentiate between them. During normal operations you’ll want the larger file/ad-blocking file named to ‘hosts.’ I recommend you backup both of these, just to be sure everything works right before you risk having to go back and recreate them.
Now all you need is to create a blank text file, and add the following:
copy hosts hosts.1
copy hosts.2 hosts
copy hosts.1 hosts.2
Rename the file to (whatever).bat and place it in the \WINDOWS\system32\drivers\etc directory. It can be run via a shortcut, by double clicking, or the command line. Hell, if you needed to have a scheduled process run with the alternate hosts file, you could even schedule this to run before and after the process via the “at” command, or the windows task scheduler.
You can determine which hosts file is loaded based on the filesize of the hosts file. If it’s 1K, it’s the default/”empty,” larger, it’s your ad-blocking hosts file.
I don’t know much about batch files, so there may be a way to improve this to allow you to recognize which one is loaded without the use of filesizes. Please feel free to add a comment if you know a superior method than this.