本帖最后由 tmplinshi 于 2011-3-24 11:09 编辑
- @echo off
- SetLocal
- set hosts=C:\Windows\System32\Drivers\etc\hosts
- if not exist %hosts% exit /b
-
- rem 把 ::ListStart 后面的行的域名赋值为 IP 地址
- set .start=
- for /f "usebackq tokens=1,2" %%a in ("%~f0") do (
- if not defined .start (
- if "%%a"=="::ListStart" set .start=#
- ) else (
- set ?%%b=%%a
- )
- )
-
- rem 如果 hosts 文件含有 隐藏/系统/只读 属性,则去除
- for %%i in (%hosts%) do set var=%%~ai
- set var=%var:-=%
- if "%var:a=%"=="" goto _del
- set var=%var:a=%
- set att=
- set att=%var:r=-r %
- set att=%att:s=-s %
- set att=%att:h=-h %
- attrib %att% %hosts%
-
- :_del
- rem 删除重复
- for /f "delims=" %%a in (' type %hosts% ') do (
- if not defined //d set //d= >%hosts%
- for /f "tokens=1,2" %%i in ("%%a") do (
- if "%%i"=="#" (
- echo %%a
- ) else (
- if not defined ?%%j echo %%i %%j
- )
- ) >>%hosts%
- )
-
- rem 添加
- for /f "tokens=1,2 delims=?=" %%a in (' set ? ') do (
- >>%hosts% echo %%b %%a
- )
-
- rem 如果之前修改了 hosts 的文件属性,则恢复原有属性
- if defined att attrib %att:-=+% %hosts%
-
- exit /b
-
- ::ListStart
-
- ; ------------- 在下面输入要添加的条目 -------------
- 6.6.6.6 xxx.com
- 127.0.0.1 baidu.com
复制代码
|