title: Batch File to Modify HOSTS File
body: <p>Windows 10 batch file:</p>
  
  <p>I wrote what I thought was a simple batch (.bat) file so that when I modify my computers HOSTS file, I could just run the batch file and copy it in place.  I usually rename the existing hosts file for backup.  The issue seems to be on the Copy command ... some sort of Access Denied (I believe). In a nut shell, I'm editing HOSTS.txt.  My batch file renames this to HOSTS, copies to proper location, then renames it (in my local folder) back to HOSTS.txt.  In between, I backup the existing hosts files saving the last 3-versions that I've modified.</p>
  
  <p>Code is below:</p>
  
  <pre><code>  IF "%OS%"=="Windows_NT" ( 
    Set sDestination="%windir%\System32\Drivers\etc"
  ) Else (
    Set sDestination="%winbootdir%\Hosts")
  Goto DoWork
  
  :DoWork
  Ren HOSTS.txt   HOSTS
  echo Backing Up Existing HOSTS Files!
  IF EXIST %sDestination%\HOSTS*.* ATTRIB +A -H -R -S %sDestination%\HOSTS*.* &gt; NUL
  IF EXIST %sDestination%\HOSTS-3.txt DEL %sDestination%\HOSTS-3.txt &gt; NUL
  IF EXIST %sDestination%\HOSTS-2.txt REN %sDestination%\HOSTS-2.txt HOSTS-3.txt &gt; NUL
  IF EXIST %sDestination%\HOSTS-1.txt REN %sDestination%\HOSTS-1.txt HOSTS-2.txt &gt; NUL
  IF EXIST %sDestination%\HOSTS REN %sDestination%\HOSTS HOSTS-1.txt &gt; NUL
  
  echo Copy Modified HOSTS file to: %sDestination%!
  IF EXIST %sDestination%\NUL COPY /Y HOSTS %sDestination%\
  
  ::Rename file back to original name
  echo Cleanning Up........
  Ren HOSTS HOSTS.txt
  GOTO Success
  </code></pre>