Use batch to delete lines of specific content in hosts

Time:2021-11-17

Q: Use bat batch to delete or modify the entries specified in hosts, for example, for 127.0.0.1   Localhosts, can you delete or change all the records with local in hosts?

A:

Copy codeThe code is as follows:
cd /d %windir%\system32\drivers\etc 
rem   Delete hosts.bak first to prevent renaming failure  
del hosts.bak 
ren hosts hosts.bak 
for /f “eol=# tokens=1,2” %%i in (hosts.bak) do call :checkvalue %%i %%j 
goto end 

:checkvalue 
echo %2 | find /i “local” 
if %errorlevel%==1 (echo %1 %2 >>hosts) 

:end