Script to add hosts ip from a txt file


  1. Posts : 8
    Windows 10
       #1

    Script to add hosts ip from a txt file


    I need a scritp to add multiple lines to hosts file from a txt file.
    I have a script but it doesn't work, it deletes all HOSTS ip and just write the last one entry, someone can help me to fix it?
    @echo off
    TITLE Modifying your HOSTS file
    ECHO.

    :: 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' (
    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%\System32\drivers\etc\storedhosts.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%\System32\drivers\etc\hosts | 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%\System32\drivers\etc\hosts %WINDIR%\System32\drivers\etc\hosts
    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
    127.0.0.1 yoursite.com
    127.0.0.1 internalsite.com
      My Computer


  2. Posts : 8,130
    windows 10
       #2

    Poweshell
    # Define the path to the text file containing IP addresses
    $filePath = "C:\path\to\your\textfile.txt"

    # Check if the file exists
    if (Test-Path $filePath) {
    # Read the content of the file
    $ipAddresses = Get-Content $filePath

    # Iterate through each IP address in the file
    foreach ($ip in $ipAddresses) {
    # Add each IP address to the hosts file
    Add-Content -Path "C:\Windows\System32\drivers\etc\hosts" -Value "$ip`thostname"
    }

    Write-Host "IP addresses added to hosts file successfully."
    } else {
    Write-Host "File not found at $filePath. Please provide the correct path."
    }
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:35.
Find Us




Windows 10 Forums