Edit Hosts File with VB Script (VBS)

November 30, 2010

The easiest way to block websites on your home computer is through the hosts file or you may also use OpenDNS for blocking adult sites.

However, when you wish to block time-wasting sites only for a temporary period, this VB Script should help. Run it once to block the site and run it again to unblock them.

 <span style="color: #606060;">   0:</span> &nbsp;
 <span style="color: #606060;">   1:</span> <span style="color: #008000;">'Add the list of time wasting websites here</span>
 <span style="color: #606060;">   2:</span> WebsitesToBlock = Array(<span style="color: #006080;">"twitter.com"</span>, <span style="color: #006080;">"www.youtube.com"</span>, <span style="color: #006080;">"www.facebook.com"</span>)
 <span style="color: #606060;">   3:</span> &nbsp;
 <span style="color: #606060;">   4:</span> &nbsp;
 <span style="color: #606060;">   5:</span> <span style="color: #008000;">'-------------------------------------------------------------------</span>
 <span style="color: #606060;">   6:</span> <span style="color: #008000;">' LetMeWork.vbs - Block Time Wasting Websites</span>
 <span style="color: #606060;">   7:</span> <span style="color: #008000;">' Author: Amit Agarwal http://www.labnol.org/</span>
 <span style="color: #606060;">   8:</span> <span style="color: #008000;">' This script will block /unblock time wasting sites with a click.</span>
 <span style="color: #606060;">   9:</span> <span style="color: #008000;">' Internally, it will edit the Windows hosts file on your computer.</span>
 <span style="color: #606060;">  10:</span> <span style="color: #008000;">' For suggestions, send me an email at amit@labnol.org</span>
 <span style="color: #606060;">  11:</span> <span style="color: #008000;">'-------------------------------------------------------------------</span>
 <span style="color: #606060;">  12:</span> <span style="color: #008000;">'-------------------------------------------------------------------</span>
 <span style="color: #606060;">  13:</span> &nbsp;
 <span style="color: #606060;">  14:</span> <span style="color: #0000ff;">If</span> WScript.Arguments.length =0 <span style="color: #0000ff;">Then</span>
 <span style="color: #606060;">  15:</span>  <span style="color: #0000ff;">Set</span> objShell = CreateObject(<span style="color: #006080;">"Shell.Application"</span>)
 <span style="color: #606060;">  16:</span>     objShell.ShellExecute <span style="color: #006080;">"wscript.exe"</span>, Chr(34) &amp; WScript.ScriptFullName &amp; Chr(34) &amp; <span style="color: #006080;">" RunAsAdministrator"</span>, <span style="color: #006080;">""</span>, <span style="color: #006080;">"runas"</span>, 1
 <span style="color: #606060;">  17:</span> <span style="color: #0000ff;">Else</span>
 <span style="color: #606060;">  18:</span>  <span style="color: #0000ff;">Const</span> ForReading = 1, ForWriting = 2
 <span style="color: #606060;">  19:</span>     
 <span style="color: #606060;">  20:</span>  <span style="color: #0000ff;">Set</span> shell = CreateObject(<span style="color: #006080;">"WScript.Shell"</span>)    
 <span style="color: #606060;">  21:</span>     root = shell.ExpandEnvironmentStrings(<span style="color: #006080;">"%systemroot%"</span>)     
 <span style="color: #606060;">  22:</span>     hostFile = root &amp; <span style="color: #006080;">"\system32\drivers\etc\hosts"</span>
 <span style="color: #606060;">  23:</span>     tempFile = hostFile &amp; <span style="color: #006080;">".bak"</span>
 <span style="color: #606060;">  24:</span>     
 <span style="color: #606060;">  25:</span>     blocked = 0
 <span style="color: #606060;">  26:</span>     towrite = <span style="color: #0000ff;">false</span>
 <span style="color: #606060;">  27:</span>     
 <span style="color: #606060;">  28:</span>  <span style="color: #0000ff;">Set</span> fso = CreateObject(<span style="color: #006080;">"Scripting.FileSystemObject"</span>)
 <span style="color: #606060;">  29:</span>  <span style="color: #0000ff;">Set</span> f1 = fso.OpenTextFile(hostFile, ForReading, <span style="color: #0000ff;">True</span>)
 <span style="color: #606060;">  30:</span>  <span style="color: #0000ff;">Set</span> f2 = fso.OpenTextFile(tempFile, ForWriting, <span style="color: #0000ff;">True</span>)
 <span style="color: #606060;">  31:</span>     
 <span style="color: #606060;">  32:</span>  <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">Until</span> f1.AtEndOfStream
 <span style="color: #606060;">  33:</span>     
 <span style="color: #606060;">  34:</span>         line = f1.Readline
 <span style="color: #606060;">  35:</span>         towrite = <span style="color: #0000ff;">true</span>
 <span style="color: #606060;">  36:</span>     
 <span style="color: #606060;">  37:</span>      <span style="color: #0000ff;">For</span> <span style="color: #0000ff;">Each</span> URL <span style="color: #0000ff;">in</span> WebsitesToBlock
 <span style="color: #606060;">  38:</span>          <span style="color: #0000ff;">If</span> instr(line, URL) <span style="color: #0000ff;">Then</span>
 <span style="color: #606060;">  39:</span>              <span style="color: #0000ff;">If</span> blocked = 0 <span style="color: #0000ff;">Then</span> 
 <span style="color: #606060;">  40:</span>                  <span style="color: #0000ff;">If</span> left(line, 1) = <span style="color: #006080;">"#"</span> <span style="color: #0000ff;">Then</span> blocked = 1 <span style="color: #0000ff;">Else</span> blocked = 2
 <span style="color: #606060;">  41:</span>              <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
 <span style="color: #606060;">  42:</span>             towrite = <span style="color: #0000ff;">false</span>
 <span style="color: #606060;">  43:</span>          <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
 <span style="color: #606060;">  44:</span>      <span style="color: #0000ff;">Next</span>    
 <span style="color: #606060;">  45:</span>         
 <span style="color: #606060;">  46:</span>      <span style="color: #0000ff;">If</span> towrite <span style="color: #0000ff;">Then</span> f2.WriteLine line
 <span style="color: #606060;">  47:</span>  <span style="color: #0000ff;">Loop</span>
 <span style="color: #606060;">  48:</span>     
 <span style="color: #606060;">  49:</span>  <span style="color: #0000ff;">For</span> <span style="color: #0000ff;">Each</span> URL <span style="color: #0000ff;">in</span> WebsitesToBlock
 <span style="color: #606060;">  50:</span>      <span style="color: #0000ff;">If</span> blocked &lt;&gt; 2 <span style="color: #0000ff;">Then</span>
 <span style="color: #606060;">  51:</span>             f2.WriteLine <span style="color: #006080;">"127.0.0.1"</span> &amp; vbTab &amp; vbTab &amp; URL 
 <span style="color: #606060;">  52:</span>      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
 <span style="color: #606060;">  53:</span>  <span style="color: #0000ff;">Next</span>
 <span style="color: #606060;">  54:</span>     
 <span style="color: #606060;">  55:</span>     fso.Copyfile tempFile, hostFile
 <span style="color: #606060;">  56:</span>     
 <span style="color: #606060;">  57:</span>     f1.Close
 <span style="color: #606060;">  58:</span>     f2.Close
 <span style="color: #606060;">  59:</span>     
 <span style="color: #606060;">  60:</span>  <span style="color: #0000ff;">If</span> blocked = 2 <span style="color: #0000ff;">Then</span> 
 <span style="color: #606060;">  61:</span>         WScript.echo <span style="color: #006080;">"Time wasting websites have now been unblocked!"</span> 
 <span style="color: #606060;">  62:</span>  <span style="color: #0000ff;">Else</span>
 <span style="color: #606060;">  63:</span>         WScript.echo <span style="color: #006080;">"Time wasting websites are now blocked!"</span> 
 <span style="color: #606060;">  64:</span>  <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
 <span style="color: #606060;">  65:</span> &nbsp;
 <span style="color: #606060;">  66:</span> <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
 <span style="color: #606060;">  67:</span> &nbsp;
 <span style="color: #606060;">  68:</span> <span style="color: #008000;">' By Amit Agarwal http://www.labnol.org/</span>
 <span style="color: #606060;">  69:</span> &nbsp;