Windows Batch:如何添加主机条目? [英] Windows Batch: How to add Host-Entries?

查看:34
本文介绍了Windows Batch:如何添加主机条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个批处理脚本通过使用 批处理.

I want to use this batch script to add new entries into my host file automatically by using windows batch.

不幸的是,脚本只在主机文件中添加了一行,当我以管理员身份运行脚本时,还有什么问题?

Unfortunately, the script just adds one single line to the hosts file, also when i run the script as a administrator, so what's wrong?

@echo off

set hostspath=%windir%System32driversetchosts

echo 62.116.159.4 ns1.intranet.de >> %hostspath%
echo 217.160.113.37 ns2.intranet.de >> %hostpath%
echo 89.146.248.4 ns3.intranet.de >> %hostpath%
echo 74.208.254.4 ns4.intranet.de >> %hostpath%

exit

推荐答案

我会这样做,所以如果脚本多次运行,你最终不会得到重复的条目.

I would do it this way, so you won't end up with duplicate entries if the script is run multiple times.

@echo off

SET NEWLINE=^& echo.

FIND /C /I "ns1.intranet.de" %WINDIR%system32driversetchosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^62.116.159.4 ns1.intranet.de>>%WINDIR%System32driversetchosts

FIND /C /I "ns2.intranet.de" %WINDIR%system32driversetchosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^217.160.113.37 ns2.intranet.de>>%WINDIR%System32driversetchosts

FIND /C /I "ns3.intranet.de" %WINDIR%system32driversetchosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^89.146.248.4 ns3.intranet.de>>%WINDIR%System32driversetchosts

FIND /C /I "ns4.intranet.de" %WINDIR%system32driversetchosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^74.208.254.4 ns4.intranet.de>>%WINDIR%System32driversetchosts

这篇关于Windows Batch:如何添加主机条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆