1 @echo off 2 color 0a 3 title hosts appender 1.0 4 5 SETLOCAL ENABLEDELAYEDEXPANSION 6 set /a RTN_VAL=0 7 set ORIGIN_HOSTS=C:\Windows\system32\drivers\etc\hosts 8 set FILE_HOSTS=hosts.tmp 9 set FOLDER_HOSTS=hosts.tmp.1 10 11 if not exist "%ORIGIN_HOSTS%" ( 12 echo Not exist hosts. 13 goto NORMAL_EXIT 14 ) 15 16 set /a RTN_VAL=0 17 cd /d "%ORIGIN_HOSTS%" && set /a RTN_VAL=1 18 if !RTN_VAL! equ 1 ( 19 echo Not a hosts file. 20 goto NORMAL_EXIT 21 ) 22 cls 23 24 if "%TEMP%" neq "" ( 25 cd /d "%TEMP%" 26 set /a RTN_VAL=0 27 cd . > "%FILE_HOSTS%" || set /a RTN_VAL=1 28 if !RTN_VAL! equ 1 ( 29 cls 30 echo Can not access to "%FILE_HOSTS%". 31 goto NORMAL_EXIT 32 ) 33 ) else ( 34 echo Environment Variables - TEMP is empty. 35 goto NORMAL_EXIT 36 ) 37 38 :INPUT_COMMENTS 39 set /p COMMENTS=Enter comments: 40 if "%COMMENTS%" equ "" ( 41 goto INPUT_COMMENTS 42 ) 43 :INPUT_IP_ADDRESS 44 set /p IP_ADDRESS=Enter IP address: 45 if "%IP_ADDRESS%" equ "" ( 46 goto INPUT_IP_ADDRESS 47 ) 48 :INPUT_HOST_NAMES 49 set /p HOST_NAMES=Enter host names: 50 if "%HOST_NAMES%" equ "" ( 51 goto INPUT_HOST_NAMES 52 ) 53 54 >> "%FILE_HOSTS%" echo. 55 >> "%FILE_HOSTS%" echo # %COMMENTS% 56 >> "%FILE_HOSTS%" echo %IP_ADDRESS% %HOST_NAMES% 57 58 cls 59 echo Preview: 60 type "%FILE_HOSTS%" 61 echo. 62 63 :INPUT_APPEND_CONFIRM 64 set /p APPEND_CONFIRM=Enter 'Y' append to origin hosts, or 'n' quit.": 65 if "%APPEND_CONFIRM%" equ "Y" ( 66 set /a RTN_VAL=0 67 type "%FILE_HOSTS%" >> "%ORIGIN_HOSTS%" || set /a RTN_VAL=1 68 if !RTN_VAL! equ 0 ( 69 echo Completed successfully. 70 goto NORMAL_EXIT 71 ) else ( 72 cls 73 if exist "%FOLDER_HOSTS%" ( 74 rmdir /s /q "%FOLDER_HOSTS%" 75 ) 76 mkdir "%FOLDER_HOSTS%" 77 type "%ORIGIN_HOSTS%" > "%FOLDER_HOSTS%\hosts" 78 type "%FILE_HOSTS%" >> "%FOLDER_HOSTS%\hosts" 79 80 set ORIGIN_ATTR= 81 for /f "tokens=*" %%I in ("%ORIGIN_HOSTS%") do ( 82 set ORIGIN_ATTR=%%~aI 83 ) 84 if "!ORIGIN_ATTR!" neq "" ( 85 set /a RTN_VAL=0 86 for /f "tokens=1* delims=-" %%m in ("!ORIGIN_ATTR!") do ( 87 set ORIGIN_ATTR=%%m 88 set /a RTN_VAL=1 89 ) 90 if !RTN_VAL! equ 0 ( 91 set ORIGIN_ATTR= 92 ) 93 ) else ( 94 echo Can not obtain file attributes. 95 goto NORMAL_EXIT 96 ) 97 attrib -r -a -s -h -i "%FOLDER_HOSTS%\hosts" 98 set SURPLUS=!ORIGIN_ATTR! 99 set STR_ATTR= 100 :PROCESS_ATTR 101 for /f "tokens=*" %%m in ("!SURPLUS!") do ( 102 set STR_ATTR=!STR_ATTR!+!SURPLUS:~0,1! 103 set SURPLUS=!SURPLUS:~1! 104 ) 105 if "!SURPLUS!" neq "" ( 106 goto PROCESS_ATTR 107 ) 108 if "!STR_ATTR!" neq "" ( 109 rem No 'I'. 110 attrib !STR_ATTR!"%FOLDER_HOSTS%\hosts" 111 ) 112 113 set /a RTN_VAL=0 114 xcopy /y /q /h /r /k "%FOLDER_HOSTS%\hosts" "%ORIGIN_HOSTS%" || set /a RTN_VAL=1 115 if !RTN_VAL! equ 0 ( 116 cls 117 echo Completed successfully. 118 goto NORMAL_EXIT 119 ) else ( 120 echo Failed. 121 goto NORMAL_EXIT 122 ) 123 ) 124 ) else if "%APPEND_CONFIRM%" equ "n" ( 125 goto NORMAL_EXIT 126 ) else ( 127 goto INPUT_APPEND_CONFIRM 128 ) 129 130 :NORMAL_EXIT 131 pause 132 exit