Batch process add multiple new domain names to Hosts_dos/bat

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.