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

Apa*_*ria 2 hosts batch-file

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

我想以这样的方式使用批处理脚本:当我运行我的时example.bat,它首先检查要添加的行是否存在,如果它们不存在则添加它们.但批处理文件应删除已存在于hosts文件中的行.换句话说,批处理文件应该切换文件中行的存在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
Run Code Online (Sandbox Code Playgroud)

wOx*_*xOm 5

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

@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
Run Code Online (Sandbox Code Playgroud)