如何编辑使用批处理文件主机[检查线路,如果犯规存在添加,删除,如果确实] [英] How to edit Hosts using Batch file[Check for line,add if doesnt exist,delete if does]

查看:139
本文介绍了如何编辑使用批处理文件主机[检查线路,如果犯规存在添加,删除,如果确实]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本几行添加到我的hosts文件,阻止某些网站在我computer.I想用批处理脚本以这样的方式,当我跑我Example.bat它第一次检查,看是否线加入即时通讯存在的,如果他们不它,然后将它们相加。
但如果他们这样做存在,删除它们。(基本上像一个切换?)

我如何做到这一点?

下面是我迄今为止,它是所有添加的行。

 关闭@echo:: BatchGotAdmin
:-------------------------------
REM - >检查权限
> NUL 2 - ;&安培; 1%SYSTEMROOT%\\ SYSTEM32 \\ CACLS.EXE%SYSTEMROOT%\\ system32 \\设置\\系统REM - >如果错误标志设置,我们没有联系。
如果%ERRORLEVEL%NEQ0(
    转到UACPrompt
)其他(转到gotAdmin):UACPrompt
    回声设置UAC = ^的CreateObject(Shell.Application^)>%temp%\\ getadmin.vbs
    集PARAMS =%*:=
    回声UAC.ShellExecutecmd.exe的,/ C%〜S0%PARAMS%,,运行方式,1>> %temp%\\ getadmin.vbs    %temp%\\ getadmin.vbs
    德尔%temp%\\ getadmin.vbs
    出口/ B:gotAdmin
    PUSHD%CD%
    CD / D%〜DP0
:--------------------------------------关闭@echo集hostspath =%WINDIR%\\ SYSTEM32 \\ DRIVERS \\ ETC \\主机呼应127.0.0.1 www.example1.com>> %hostspath%
呼应127.0.0.1 www.example2.com>> %hostspath%
呼应127.0.0.1 www.example3.com>> %hostspath%出口


解决方案

一个纯一批code带解释性意见:

 关闭@echo
SETLOCAL enableDelayedExpansionREM双引号的情况下WINDIR变量里面有空格O_O
设置hostspath =%WINDIR%\\ SYSTEM32 \\ DRIVERS \\等\\东道主REM初始化我们的东道主的阵列切换
对于一个%%的(
    127.0.0.1 www.example1.com
    127.0.0.1 www.example2.com
    127.0.0.1 www.example3.com
)做(
    集/ A numhosts + = 1
    设置主持人!numhosts!= %%〜一
)>%hostspath%。新(
    REM解析hosts文件,从我们的名单跳过已经present主机。
    REM空白行使用FINDSTR招preserved。
    FOR / Fdelims =:代币= 1 *%%一个在('FINDSTR / N / R /c:\".*%hostspath%')做(
        设置skipline =
        在/ L %% H(1,1,!numhosts!)做(
            如果%% B==!主持人%% ^ h! (
                集skipline = TRUE
                集中找到%% H =真
                回声 - %% b 1分配>和2
            )
        )
        如果不是!skipline!==真正的回音。%% b
    )
    在/ L %% H(1,1,!numhosts!)做(
        如果没有!找到%% ^ h!==真正的回声+!主机%% ^ h! 1 GT;&放大器; 2及回音!主机%% ^ h!
    )

移动/ Y%hostspath%%hostspath%的.bak> NUL ||回声无法备份%hostspath%
移动/ Y%hostspath%。新%hostspath%GT; NUL ||回声无法更新%hostspath%
暂停

I have a Batch Script to add several lines to my hosts file to block certain websites on my computer.I would like to Use batch script in such a way that when i run my Example.bat it 1st checks to see if the lines im adding exist,and if they dont it then adds them. but if they do exist it deletes them.(basically like a toggle?)

How do i do this?

Here is what i have so far,all it does is add the lines.

@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 admin.
if '%errorlevel%' NEQ '0' (
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > 

"%temp%\getadmin.vbs"
    set 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=%windir%\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 

解决方案

A pure batch code with explanatory comments:

@echo off
setlocal enableDelayedExpansion

rem Doublequotes inside the variable in case WINDIR has spaces O_O
set hostspath="%windir%\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 ('findstr /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%
pause

这篇关于如何编辑使用批处理文件主机[检查线路,如果犯规存在添加,删除,如果确实]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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