Requirements: Batch (. bat) add more than one new domain name to hosts, first detect if the existing domain name is not added
Other domain names that do not already exist, add directly
Google search is there to find the code as follows:
Copy Code code as follows:
Set sthosts= x.x.x.x www.domainname.com
for/f "eol=# Tokens=1 delims="%%i in (%systemroot%\system32\drivers\etc\hosts) do if "%sthosts%" = "%%i" exit
Echo%sthosts%>>%systemroot%\system32\drivers\etc\hosts
However, this code can only detect one row of domain names
How can you rewrite code to detect multiple domain names if you want to add the following various domain addresses?
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
Method One:
@echo off & setlocal enabledelayedexpansion
Set "file=%systemroot%\system32\drivers\etc\hosts"
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!") does if not defined #%%k s!
)
More +11 "%~0"
Pause & Exit
Method Two: With the detection function (various domain addresses are written in a file named List.txt, one line)
@echo off
if exist%temp%\$ del/q%temp%\$
for/f ' delims= '%%i in (%systemroot%\system32\drivers\etc\hosts) do E Cho%%i>>%temp%\$
for/f "delims="%%a in (' findstr/v/g: "%temp%\$" List.txt ") do echo%%a>>%systemroot% \system32\drivers\etc\hosts