Batch file 如何使用批处理文件编辑主机文件(检查行,如果不存在则添加,如果存在则删除)?

Batch file 如何使用批处理文件编辑主机文件(检查行,如果不存在则添加,如果存在则删除)?,batch-file,hosts,Batch File,Hosts,我有一个批处理脚本,可以在我的主机文件中添加几行代码,以阻止计算机上的某些网站 我希望以这样的方式使用批处理脚本:当我运行example.bat时,它首先检查要添加的行是否存在,如果不存在,然后添加它们。但是,如果主机文件中已有行,则批处理文件应删除这些行。换句话说,批处理文件应该切换hosts文件中行的存在 这怎么可能呢 这是我到目前为止所拥有的。它所做的只是添加行 @echo off :: BatchGotAdmin ::----------------------------------

我有一个批处理脚本,可以在我的主机文件中添加几行代码,以阻止计算机上的某些网站

我希望以这样的方式使用批处理脚本:当我运行
example.bat
时,它首先检查要添加的行是否存在,如果不存在,然后添加它们。但是,如果
主机
文件中已有行,则批处理文件应删除这些行。换句话说,批处理文件应该切换
hosts
文件中行的存在

这怎么可能呢

这是我到目前为止所拥有的。它所做的只是添加行

@echo off

:: BatchGotAdmin
::-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"

REM --> If error flag set, we do not have administrator privileges.
if not errorlevel 1 goto gotAdmin

echo Set UAC = CreateObject^("Shell.Application"^) >"%temp%\getadmin.vbs"
set params=%*
if defined params set params=%params:"=""%
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::--------------------------------------

@echo off

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

echo 127.0.0.1 www.example1.com >> %hostspath%
echo 127.0.0.1 www.example2.com >> %hostspath%
echo 127.0.0.1 www.example3.com >> %hostspath%

exit

带有解释性注释的纯批处理代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "hostspath=%SystemRoot%\System32\drivers\etc\hosts"

rem Initialize the array of our hosts to toggle
for %%a in (
    "127.0.0.1 www.example1.com"
    "127.0.0.1 www.example2.com"
    "127.0.0.1 www.example3.com"
) do (
    set /a numhosts+=1
    set "host!numhosts!=%%~a"
)

>"%hostspath%.new" (
    rem Parse the hosts file, skipping the already present hosts from our list.
    rem Blank lines are preserved using findstr trick.
    for /f "delims=: tokens=1*" %%a in ('%SystemRoot%\System32\findstr.exe /n /r /c:".*" "%hostspath%"') do (
        set skipline=
        for /L %%h in (1,1,!numhosts!) do (
            if "%%b"=="!host%%h!" (
                set skipline=true
                set found%%h=true
                echo - %%b 1>&2
            )
        )
        if not "!skipline!"=="true" echo.%%b
    )
    for /L %%h in (1,1,!numhosts!) do (
        if not "!found%%h!"=="true" echo + !host%%h! 1>&2 & echo !host%%h!
    )
)
move /y "%hostspath%" "%hostspath%.bak" >nul || echo Can't backup %hostspath%
move /y "%hostspath%.new" "%hostspath%" >nul || echo Can't update %hostspath%
endlocal
pause

要成为舒尔,外壳无关紧要,我会将
如果“%%b”==“!host%%h!”
更改为
如果/I”%%b”=“!host%%h!”
并在开始时重置numhosts。