【问题标题】:Making a batch file for adding/removing specific lines to hosts file制作批处理文件以向主机文件添加/删除特定行
【发布时间】: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


    【解决方案1】:
    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    :ACCEPTED
    setlocal enabledelayedexpansion
    SET "filename1=%WINDIR%\System32\drivers\etc\hosts"
    SET "filename1=q34775003.txt"
    ::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
    
    ECHO Carrying out requested modifications to your HOSTS file
    :: remove existing names from hosts file
    
    findstr /v /e "%LIST:~1,-1%" "%filename1%"> tmp.txt
    
    :: Add new list
    for  %%G in %list% do (
    rem    set  _name=%%G
    rem    set  _value=!%%G!
        ECHO !%%G! %%G>>tmp.txt
    )
    ::overwrite host file
    move tmp.txt %filename1% >nul
    )
    

    在块语句中应使用(a parenthesised series of statements)REM 语句而不是损坏标签的备注形式 (:: comment),因为标签终止块,使 cmd 混淆。

    这是一个完整的修订版,因为我在解释原始批次所做的事情时搞砸了。

    它旨在替换:ACCEPTED 例程 - 您需要重新插入ipconfig 命令

    filename1 设置为实际文件位置,然后设置为测试文件位置进行测试。您需要删除多余的设置。

    首先,删除现有的名称条目并创建temp.txt 其余部分。要匹配(并因此被删除),名称必须在行尾匹配,因此 a.ppy.sh 将被删除,但 a.ppy.shx 将被保留。

    接下来,将新条目附加到temp.txt

    最后,move 替换了原来的文件。

    所有需要做的就是使用一个虚拟文件进行测试,并在得到证明后,像原始文件一样附加ipconfig...

    最后,

    【讨论】:

    • 我将如何在脚本中实现它?它可以自己运行吗,因为即使在管理员中运行它似乎也不会删除这些行。虽然我可能是错的,因为我真的不知道我在做什么。非常感谢您的帮助。
    【解决方案2】:

    如果您打算编写或删除相同的四行,为什么不创建和维护两个文件(短版和长版)?然后根据你要做什么复制对应的文件?

    例子:

    pushd %systemroot%\system32\drivers\etc
    choice /C:SL "Choose S for short or L for long."
    IF errorlevel 2 goto long
    IF errorlevel 1 goto short
    
    :long
    copy hosts.long hosts
    goto end
    
    :short
    copy hosts.short hosts
    goto end
    
    :end
    popd
    

    就个人而言,我会使用 PowerShell,但您确实询问了批处理文件。

    【讨论】:

    • 要么工作,要么工作,我只需要能够轻松分发它而无需太多设置。我正在使用类似于您之前建议的东西 pastebin.com/jNFcmULL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2023-01-10
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多