The hosts file is read only protected. Below Bat script is what I use to change the r attribute and edit the file. Ok I use Notepad++ and have Set a variable for OneDrive... Here's the script... Have fun.
@echo off
SetLocal EnableDelayedExpansion EnableExtensions
:: +-----------------------------------------------------------------+
:: | Author: Bob Maarschalkerweerd - 2018
:: | Edit the hosts file, change attributes etc and save
:: | Bats and files used:
:: | 1) %~dp0EditHostsFile.bat
:: | 2) echo Edit Hosts File&START "EditHosts" /D "%OneDrive%" /High "%OneDrive%\Batch\EditHostsFile.bat"
:: | Last Updated: 02 May 2018 08:49 AM
:: | Last Updated: 02 May 2018 08:49 AM
:: +-----------------------------------------------------------------+
title=[%~nx0] %date% %time% - Edit the hosts file, change attributes etc and save
title %title%
SET In=C:\Windows\System32\drivers\etc\
SET Ed="C:\Program Files\Notepad++\notepad++.exe"
c:
cd\
cd %In%
echo %date% %time% ----------------------------------------------------------------------------------------
echo %date% %time% -----{ attrib -r %In%\hosts }-----
set /a c=!c!+1
eventcreate /l Application /so Hostfile /t information /id 540 /d "attrib -r %In%\hosts" > NUL 2>&1
attrib -r %In%\hosts
echo %date% %time% -----{ !c!) ERRORLEVEL: %ERRORLEVEL% }-----
if %errorlevel% neq 0 (
eventcreate /l Application /so Hostfile /t error /id 544 /d "attrib -r %In%\hosts" > NUL 2>&1
echo %date% %time% -----{ attrib -r ERRORLEVEL: %errorlevel% }-----
)
echo %date% %time% ----------------------------------------------------------------------------------------
echo %date% %time% -----{ call %Ed% hosts }-----
set /a c=!c!+1
eventcreate /l Application /so Hostfile /t information /id 541 /d "call "c:\Program Files\Notepad++\notepad++.exe" hosts" > NUL 2>&1
call "c:\Program Files\Notepad++\notepad++.exe" hosts
echo %date% %time% -----{ !c!) ERRORLEVEL: %ERRORLEVEL% }-----
if %errorlevel% neq 0 (
eventcreate /l Application /so Hostfile /t error /id 544 /d "call "c:\Program Files\Notepad++\notepad++.exe" hosts"" > NUL 2>&1
echo %date% %time% -----{ attrib -r ERRORLEVEL: %errorlevel% }-----
)
echo %date% %time% -----{ %~nx0 Paused for edit. Make changes and save first. }-----
pause
echo %date% %time% ----------------------------------------------------------------------------------------
echo %date% %time% -----{ attrib +r hosts }-----
set /a c=!c!+1
eventcreate /l Application /so Hostfile /t information /id 540 /d "attrib +r %In%\hosts" > NUL 2>&1
attrib +r %In%\hosts
echo %date% %time% -----{ !c!) ERRORLEVEL: %ERRORLEVEL% }-----
if %errorlevel% neq 0 (
eventcreate /l Application /so Hostfile /t error /id 544 /d "attrib +r %In%\hosts" > NUL 2>&1
echo %date% %time% -----{ attrib -r ERRORLEVEL: %errorlevel% }-----
)
echo %date% %time% ----------------------------------------------------------------------------------------
pause >nul
exit