欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用批处理删除HOSTS中特定内容的行的代码

程序员文章站 2022-06-16 15:54:04
q:用bat批处理来删除或者修改hosts中指定的条目,例如对于127.0.0.1 localhosts,能否把hosts里面带有local的记录全都删掉或者改掉...

q:用bat批处理来删除或者修改hosts中指定的条目,例如对于127.0.0.1 localhosts,能否把hosts里面带有local的记录全都删掉或者改掉?

a:

复制代码 代码如下:

cd /d %windir%\system32\drivers\etc 
rem 先删除hosts.bak防止重命名失败 
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