要求:批处理(.bat) 添加多个新的域名到hosts,先检测是否已存在,如果已存在的域名就不添加
其他尚未存在的域名,则直接添加
google搜寻是有找到代码 如下:
复制代码 代码如下:
set stHosts= X.X.X.X www.domainname.com
FOR /F "eol=# tokens=1 delims=" %%i in (%systemroot%system32driversetchosts) do if "%stHosts%"=="%%i" exit
echo %stHosts%>> %systemroot%system32driversetchosts
不过此代码只能检测一行域名
如果要添加以下各种不同域址,如何改写代码才能检测多个的域名?
127.0.0.1 mcfg.sandai.net
127.0.0.1 211.94.190.80
127.0.0.1 advstat.xunlei.com
123.87.86.5 biz5.sandai.net
527.66.13.125 cknum.sandai.net
110.45.215.113 cl.kankan.xunlei.com
127.0.0.1 float.sandai.net
127.0.0.1 mcfg.sandai.net
180.70.134.154 mtips.xunlei.com
127.0.0.1 pubstat.sandai.net
216.58.221.74 recommend.xunlei.com
127.0.0.1 wy.xunlei.com
110.45.229.148 buy.safe.xunlei.com
方法一:
@echo off & setlocal enabledelayedexpansion set "file=%systemroot%system32driversetchosts" for /f "tokens=2" %%i in ('more +11 "%~0"') do set #%%i=1 for /f "delims=" %%i in ('findstr /n .* %file%') do ( set "s=%%i" set "s=!s:*:=!" for /f "tokens=1,2" %%j in ("#!s!") do if not defined #%%k echo;!s! ) more +11 "%~0" pause & exit
方法二:带上检测功能(各种域址写在名为list.txt的文件中,一行一个)
@echo off if exist %temp%$ del /q %temp%$ for /f "delims=" %%i in (%systemroot%system32driversetchosts) do echo %%i>>%temp%$ for /f "delims=" %%a in ('findstr /v /g:"%temp%$" list.txt') do echo %%a>> %systemroot%system32driversetchosts