广州鸿名健康科技有限公司


批处理添加多个新的域名到hosts

网络编程 批处理添加多个新的域名到hosts 06-21

要求:批处理(.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

BAT批量去除文件首行以及批量合并文件脚本
bat批量去除文件首行setn=1:starlinefor%%jin(*.txt)do(:3ifexistD:worktestnew_%n%.txt(set/an+=1&goto3)setfile=%n%.txtfor/f"skip=1delims="%%iin('type"%file%"')do(echo%%iD:worktestnew_%n%.txt

Windows2003下批量添加和导出所有ip BAT脚本
批量添加IP在cmd命令行下运行:FOR/L%iIN(130,1,190)DOnetshinterfaceipaddaddress"本地连接"192.168.1.%i255.255.255.192往本地连接中添加192.168.1.130~192.168.1.190的所有IP,且

Windows使用bat批处理实现守护进程脚本分享
本文转自网络,由于找不到原作者,因而无法知道出处。如果有幸让原作者看到,请联系我加上。先转载至此。最近几天加班加疯掉了,天天晚上没法


编辑:广州鸿名健康科技有限公司

标签:批量,域名,文件,多个,脚本