【发布时间】:2016-04-18 22:02:03
【问题描述】:
我希望能够有一个 2 选项批处理文件,它可以写入四行,并且还能够删除那些特定的行而不擦除主机文件中的其他行。我在这里找到了一个脚本:Windows Batch: How to add Host-Entries?,我为自己的用途编辑了它,但我很困惑如何去做,因为我对编写批处理文件还很陌生。觉得你们能帮帮我吗?
@echo off
REM ...fluff removed...
:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of host domains
set LIST=(osu.ppy.sh a.ppy.sh c.ppy.sh c1.ppy.sh)
::Set the ip of the domains you set in the list above
set osu.ppy.sh=178.62.57.37
set a.ppy.sh=178.62.57.37
set c.ppy.sh=178.62.57.37
set c1.ppy.sh=178.62.57.37
:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
::ECHO %WINDIR%\System32\drivers\etc\hosts > tmp.txt
for %%G in (%_list%) do (
set _name=%%G
set _value=!%%G!
SET NEWLINE=^& echo.
ECHO Carrying out requested modifications to your HOSTS file
::strip out this specific line and store in tmp file
type %WINDIR%\System32\drivers\etc\hosts | findstr /v !_name! > tmp.txt
::re-add the line to it
ECHO %NEWLINE%^!_value! !_name!>>tmp.txt
::overwrite host file
copy /b/v/y tmp.txt %WINDIR%\System32\drivers\etc\hosts
del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
GOTO END
:END
EXIT
【问题讨论】:
标签: batch-file hosts