标题: [网络连接] 批处理解决window7 hosts被注释问题 [打印本页]
作者: wu26853767 时间: 2012-3-16 00:06 标题: 批处理解决window7 hosts被注释问题
本帖最后由 wu26853767 于 2012-3-16 11:38 编辑
windowXP系统hosts文件被注释添加#之后可以使用- @echo off
- set "hostfile=C:\WINDOWS\system32\drivers\etc\hosts"
- set "tempfile=%temp%\host.tmp"
- for /f "delims=:" %%a in ('findstr /n "^127.0.0.1" "%hostfile%"') do (
- set "num=%%a"
- )
- type nul >"%tempfile%"
- for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%hostfile%"') do (
- set "str=%%b"
- setlocal enabledelayedexpansion
- if %%a leq %num% (
- >>"%tempfile%" echo,%%b
- ) else if "!str:~0,1!" equ "#" (
- >>"%tempfile%" echo,!str:~1!
- ) else (
- >>"%tempfile%" echo,%%b
- )
- endlocal
- )
- move /y "%tempfile%" "%hostfile%"
复制代码
删除127.0.0.1以下的井号
但是window7里的hosts内容和XP不一样
window7的hosts的内容是:
# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
#192.168.1.100 www.baidu.com
请问windows7系统下的hosts文件解析被注释后怎么删除井号呢
最好是可以兼容XP的批处理
先谢过各位老大,windows7权限问题可以不考虑
作者: ivor 时间: 2012-3-16 12:30
- setlocal enabledelayedexpansion
- for /f "delims=" %%1 in (host) do (
- set temp=%%1
- set temp=!temp:#=!
- echo !temp!
- )
复制代码
作者: wu26853767 时间: 2012-3-16 12:58
本帖最后由 wu26853767 于 2012-3-16 13:00 编辑
首先感谢楼上- @echo off&&setlocal enabledelayedexpansion
- set datais=%SystemRoot%\system32\drivers\etc\
- if exist !datais!hosts_bak del !datais!hosts_bak/f/q
- for /f "tokens=*" %%a in (!datais!hosts) do (
- set var=%%a
- set "var=!var:#= !"
- >>!datais!hosts.txt echo !var!
- )
- ren !datais!hosts hosts_bak
- ren !datais!hosts.txt hosts
- echo 处理完毕。
- pause>nul
复制代码
看来只有这个办法了呗 ,用这个办法hosts井号都删除掉了 但是会出现
Copyright (c) 1993-2009 Microsoft Corp.
ECHO 处于打开状态。
This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
ECHO 处于打开状态。
This file contains the mappings of IP addresses to host names. Each
entry should be kept on an individual line. The IP address should
be placed in the first column followed by the corresponding host name.
The IP address and the host name should be separated by at least one
space.
ECHO 处于打开状态。
Additionally, comments (such as these) may be inserted on individual
lines or following the machine name denoted by a '' symbol.
ECHO 处于打开状态。
For example:
ECHO 处于打开状态。
102.54.94.97 rhino.acme.com source server
38.25.63.10 x.acme.com x client host
localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
192.168.1.100 www.baidu.com
hosts里面会插入ECHO 处于打开状态。语句
请问还有其他办法删除井号并且保持内容不变吗?
作者: apang 时间: 2012-3-16 13:33
1楼的代码删除第4行127.0.0.1前面的^符号,应该就可以了吧。
作者: wu26853767 时间: 2012-3-16 14:11
回复 4# apang - @echo off
- set "hostfile=C:\WINDOWS\system32\drivers\etc\hosts"
- set "tempfile=%temp%\host.tmp"
- for /f "delims=:" %%a in ('findstr /n "127.0.0.1 ::1 localhost" "%hostfile%"') do (
- set "num=%%a"
- )
- type nul >"%tempfile%"
- for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%hostfile%"') do (
- set "str=%%b"
- setlocal enabledelayedexpansion
- if %%a leq %num% (
- >>"%tempfile%" echo,%%b
- ) else if "!str:~0,1!" equ "#" (
- >>"%tempfile%" echo,!str:~1!
- ) else (
- >>"%tempfile%" echo,%%b
- )
- endlocal
- )
- move /y "%tempfile%" "%hostfile%"
复制代码
我发现这个样子修改之后windows7和windowsXP都可以通用了
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |