I need a cmd scritp to add multiple lines to hosts file from a txt file.
I’ve found in this post a script by Tony
@echo off
TITLE Modifying your HOSTS file
ECHO.
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%system32cacls.exe" "%SYSTEMROOT%system32configsystem"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
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"
:--------------------------------------
:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop
:REJECTED
ECHO Your HOSTS file was left unchanged.
ECHO Finished.
GOTO END
:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of host domains
for /F "tokens=1,2 delims= " %%A in (%WINDIR%System32driversetcstoredhosts.txt) do (
SET _host=%%B
SET _ip=%%A
SET NEWLINE=^& echo.
ECHO Adding !_ip! !_host!
REM REM ::strip out this specific line and store in tmp file
type %WINDIR%System32driversetchosts | findstr /v !_host! > tmp.txt
REM REM ::re-add the line to it
ECHO %NEWLINE%^!_ip! !_host! >> tmp.txt
REM ::overwrite host file
copy /b/v/y tmp.txt %WINDIR%System32driversetchosts
del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
GOTO END
:END
ECHO.
PAUSE
EXIT
Example “storedhosts.txt” (tab delimited)
127.0.0.1 mysite.com
168.1.64.2 yoursite.com
192.1.0.1 internalsite.com
But it not works form me, it delete all HOSTS file and just write the last one entry.
When script starts i get this error for every ip
FIND: Parameter format not correct" and "FINDSTR: Write error" with Pipes
Someone can help me to modify this script please? Thank you
JinKazama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.