Furvus

Edit Hosts win10

Sep 21st, 2021 (edited)
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.70 KB | None | 0 0
  1. @echo off
  2. setlocal EnableExtensions DisableDelayedExpansion
  3. title Modifying your HOSTS file
  4.  
  5. color F0
  6.  
  7. set "HOSTPATH=%SYSTEMROOT%\System32\drivers\etc\hosts"
  8. set "HOME=  127.0.0.1"
  9. set "PROMPT_TEXT=Enter the host name to"
  10. set "ACTION_TEXT[1]=add"
  11. set "ACTION_TEXT[2]=remove"
  12. set "FindEmptyLine=^ *$"
  13.  
  14. set "NewLineAppended="
  15. cls
  16. setlocal EnableDelayedExpansion
  17.  
  18.  
  19. :LOOP
  20.     echo,
  21.     echo 1. Block a host
  22.     echo 2. Remove a blocked host
  23.     echo 3. Exit
  24.     choice /C "123" /N /M "Choose an item [1, 2, 3]: "
  25.  
  26.  
  27. set "Item=%errorlevel%"
  28.  
  29. goto choice%Item%
  30.  
  31. :Common
  32.     set "HOST="
  33.     set /P "HOST=!PROMPT_TEXT! !ACTION_TEXT[%Item%]!:   "
  34.     if not defined HOST (
  35.         (goto)2>nul
  36.         goto LOOP
  37.     )
  38.     set "FindEntry=^^ *!HOME! *!HOST! *$"
  39.     set "FindEntry=!FindEntry:.=\.!"
  40.  
  41. :choice0 // User Pressed CTRL-C
  42.  
  43. :choice3
  44.     pause
  45.     exit /b
  46.  
  47. :choice1
  48.     pause
  49.     call :Common
  50.     set "HostEntry=!HOME! !HOST!"
  51.     findstr /IRC:"!FindEntry!" "!HOSTPATH!" nul && (
  52.         echo The host !HOST! is already blocked, No action taken.) || (
  53.             if not defined NewLineAppended (
  54.                     REM This will append a new line ONLY if the file does not end by LF character
  55.                     type "!HOSTPATH!" | findstr $_"!HOSTPATH!.tmp" && (
  56.                         del "!HOSTPATH!"
  57.                         ren "!HOSTPATH!.tmp" "hosts"
  58.                         set "NewLineAppended=1"
  59.                     )  
  60.             )
  61.     echo !HostEntry!>>"!HOSTPATH!"
  62.     echo The host !HOST! is blocked
  63.     pause
  64.     cls
  65.     )
  66.  
  67. goto LOOP
  68.  
  69.  
  70. :choice2
  71.     pause
  72.     call :Common
  73.     findstr /VIR /C:"!FindEntry!" /C:"!FindEmptyLine!" "!HOSTPATH!" >>"!HOSTPATH!.tmp" && (
  74.         del "!HOSTPATH!"
  75.         ren "!HOSTPATH!.tmp" "hosts"
  76.         echo The host !HOST! unblocked
  77.     )
  78.     pause
  79.     cls
  80.  
  81.  
  82. goto LOOP
  83.  
  84. exit /b
Add Comment
Please, Sign In to add comment