# .bat file to set hosts perms not working



## joelbarnard (May 10, 2012)

Hi all,

I looked around to see if I could find a post exactly like this but couldn't, so I apologize if I'm re-posting.

Please can someone tell me whats wrong with my .bat file, here it is;


```
::SCRIPT FOR ADDING BLOG DNS TO HOSTS FILE AND SETTING DEFAULT HOME PAGE 
::FIRST THE HOME PAGE
@echo off  
echo To set your home page to the blog, select 1
echo To set your home page to default, select 0
set /p input= Enter your choice : 
if %input%==1 (
REG ADD "HKCU\SOFTWARE\MICROSOFT\INTERNET EXPLORER\MAIN" /V "START PAGE" /D "http://blog.rrc/" /F
)
if %input%==0 (
REG ADD "HKCU\SOFTWARE\MICROSOFT\INTERNET EXPLORER\MAIN" /V "START PAGE" /D "http://go.microsoft.com/fwlink/?LinkId=69157" /F
)
::THEN THE HOSTS FILE
@echo off & setlocal enabledelayedexpansion
set hosts=%windir%\system32\drivers\etc\hosts
set entry=192.168.1.200 blog.rrc
echo.
findstr /i "blog.rrc" %hosts%>nul
if %errorlevel% == 0 (
echo HOST ENTRY FOR BLOG EXISTS. CHECKING ACTIVE STATUS.
findstr /i "blog.rrc" %hosts% | findstr /i "#" >nul
echo.
if !errorlevel! == 0 echo ENTRY IS INACTIVE.
if !errorlevel! == 1 echo ENTRY IS ACTIVE.
)
echo.
echo SELECT 2 TO ADD BLOG
echo SELECT 3 TO REMOVE BLOG
set /p input= ENTER YOUR CHOICE :
if %input%==2 (
findstr /i /v "blog.rrc" %hosts% >%temp%\hostback.txt
type %temp%\hostback.txt >%hosts%
echo. >>%hosts%
icacls %hosts% /grant:r "Users":(f)
echo 192.168.1.200 blog.rrc >>%hosts%
echo HOST FILE IS POINTING TO OSWALD NOW, HOORAY.
)
if %input%==3 (
echo YOU CHOSE TO DISABLE THE BLOG, HERE WE GO.
findstr /i /v "blog.rrc" %hosts% >%temp%\hostback.txt
type %temp%\hostback.txt >%hosts%
echo #192.168.1.200 blog.rrc >>%hosts%
echo done!!
Echo.. CHECKING AND CONFIRMING..
findstr /i "blog.rrc" %hosts% | findstr /i "#" >nul
echo.
if %errorlevel% == 0 echo HOST FILE DISABLED SUCCESFULLY.
)
::NOW OPEN INTERNET EXPLORER
start iexplore.exe
::THE END
```
Thanks everyone...


----------



## joelbarnard (May 10, 2012)

I didn't think my question was that hard, nobody?


----------



## dvk01 (Dec 14, 2002)

moved to software development where you are more likely to get an answer instead of tips & tricks which isn't intended to be a support board


----------



## Ent (Apr 11, 2009)

What happens when you try to run this batch file?
Are you sure that you have sufficient permissions to modify the registry and the hosts file? HKCU shoudn't be a problem, but the hosts file needs admin privileges. You may need to right click on the batch and choose "Run as Administrator" if working on Vista or Windows 7.


----------



## joelbarnard (May 10, 2012)

Thank you for your reply Ent.

When I run the file as a regular user nothing happens, if I run it in Vista as an admin it works, if i run it in 7 as admin/user it does not work at all. Is this pretty normal? I'm beginning to think it is.I was hoping icacls would handle the permissions override, is that foolish thinking?


----------



## Ent (Apr 11, 2009)

icacls need to have sufficient privileges of its own to modify the file permissions, typically either because it's run by the owner of the file or because it's run an administrator. If that weren't the case, permissions would be useless! Anyone could get access to anything by issuing an icacls command. On the other hand, when run with an admin account icacls should be unnecessary anyway.

I'm afraid that I'm a vista user, and I don't know why it works there and fails on Windows 7. I would suggest that you remove the "@echo off" line and execute the batch file from within a cmd prompt, so that you can see at what point it stops executing.


----------



## joelbarnard (May 10, 2012)

Ok that all makes perfect sense. I will run it for you on 7 and post the output for fun's sake.


----------

