Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add multiple new domain names to hosts in batch

Shulou Source: shulou.com Published: 2022-06-03 09:30:59 09月14日 Update

This article mainly explains "how to add multiple new domain names to hosts in batch processing". The explanation in this article is simple and clear, and easy to learn and understand. please follow the editor's ideas to study and learn "how to batch add multiple new domain names to hosts".

Requirement: batch process (.bat) to add multiple new domain names to hosts, first check whether they already exist, and do not add them if they already exist.

For other domain names that do not already exist, add them directly

Google search is found with the following code

The code is 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 line of domain names.

If you want to add the following different domain addresses, how can I rewrite the code to detect multiple domain names?

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 1:

@ echo off & setlocal enabledelayedexpansionset "file=%systemroot%\ system32\ drivers\ etc\ hosts" for / f "tokens=2"% I in ('more + 11 "% ~ 0"') do set #% i=1for / f "delims="%% I in ('findstr / n. *% file%') do (set "slots% I" set "slots) For / f "tokens=1,2" j in ("#! s!") Do if not defined #% k echosmarts!) more + 11 "% ~ 0" pause & exit

Method 2: bring the detection function (various domain addresses are written in a file named list.txt, one per line)

@ echo offif exist% temp%\ $del / Q% temp%\ $for / f "delims="% I in (% systemroot%\ system32\ drivers\ etc\ hosts) do echo% I > >% temp%\ $for / f "delims="% an in ('findstr / v / g: "% temp%\ $" list.txt') do echo% a > >% systemroot%\ system32\ drivers\ etc\ hosts Thank you for your reading. This is the content of "how to batch add multiple new domain names to hosts". After the study of this article, I believe you have a deeper understanding of how to add multiple new domain names to hosts in batch processing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Domain name multiple code detection learning one line content method different function not yet that is ideas situations files articles more knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Docker Redmi Xiaomi