Erik TheBadger
Erik TheBadger

Reputation: 3

Making a batch file for adding/removing specific lines to hosts file

I would like to be able to have a 2 option batch file that can write four lines and also be able to remove those specific lines without wiping the other lines in the hosts file. I have a script that I found here: Windows Batch: How to add Host-Entries? that I edited for my own uses but I'm pretty stumped on how to go about this because I'm pretty new to writing batch files. Think you guys can help me?

@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

Upvotes: 0

Views: 3750

Answers (2)

Magoo
Magoo

Reputation: 79947

@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
)

Within a block statement (a parenthesised series of statements), REM statements rather than the broken-label remark form (:: comment) should be used because labels terminate blocks, confusing cmd.

This is a complete revision because I fouled up interpreting what the original batch was doing.

It is intended that this replaces the :ACCEPTED routine - you'd need to re-insert theipconfig command

filename1 is set to the real filelocation, then to a test file location for testing. You'd need to remove the superfluous setting.

First, the existing name entries are removed and temp.txt created with the remainder. To match (and hence be removed) the name must match at the end of the line, hence a.ppy.sh would be removed, but a.ppy.shx would be retained.

Next, the new entries are appended to temp.txt

Finally, the move replaces the original file.

All that needs to be done is testing with a dummy file and when proved, append the ipconfig... lines as in the original

Finally, the

Upvotes: 1

Kory Gill
Kory Gill

Reputation: 7153

If you intend on writing or removing the same four lines, why not create and maintain two files (the short and long versions)? Then copy the corresponding file based on what you want to do?

Example:

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

Personally, I'd use PowerShell, but you did ask about batch files.

Upvotes: 0

Related Questions