gpt4 book ai didi

windows-7 - Windows 批处理 : How to add Host-Entries?

转载 作者:行者123 更新时间:2023-12-02 19:44:22 25 4
gpt4 key购买 nike

我想使用此批处理脚本通过 自动将新条目添加到我的主机文件中批处理。

不幸的是,该脚本仅向主机文件添加一行,当我以管理员身份运行该脚本时也是如此,所以出了什么问题?

@echo off

set hostspath=%windir%\System32\drivers\etc\hosts

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

最佳答案

我会这样做,这样如果脚本运行多次,您就不会得到重复的条目。

@echo off

SET NEWLINE=^& echo.

FIND /C /I "ns1.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^62.116.159.4 ns1.intranet.de>>%WINDIR%\System32\drivers\etc\hosts

FIND /C /I "ns2.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^217.160.113.37 ns2.intranet.de>>%WINDIR%\System32\drivers\etc\hosts

FIND /C /I "ns3.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^89.146.248.4 ns3.intranet.de>>%WINDIR%\System32\drivers\etc\hosts

FIND /C /I "ns4.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^74.208.254.4 ns4.intranet.de>>%WINDIR%\System32\drivers\etc\hosts

关于windows-7 - Windows 批处理 : How to add Host-Entries?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com