[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[网络连接] 批处理解决window7 hosts被注释问题

本帖最后由 wu26853767 于 2012-3-16 11:38 编辑

windowXP系统hosts文件被注释添加#之后可以使用
  1. @echo off
  2. set "hostfile=C:\WINDOWS\system32\drivers\etc\hosts"
  3. set "tempfile=%temp%\host.tmp"
  4. for /f "delims=:" %%a in ('findstr /n "^127.0.0.1" "%hostfile%"') do (
  5.     set "num=%%a"
  6. )
  7. type nul >"%tempfile%"
  8. for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%hostfile%"') do (
  9.     set "str=%%b"
  10.     setlocal enabledelayedexpansion
  11.     if %%a leq %num% (
  12.         >>"%tempfile%" echo,%%b
  13.     ) else if "!str:~0,1!" equ "#" (
  14.         >>"%tempfile%" echo,!str:~1!
  15.     ) else (
  16.         >>"%tempfile%" echo,%%b
  17.     )
  18.     endlocal
  19. )
  20. 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权限问题可以不考虑

  1. setlocal enabledelayedexpansion
  2. for /f "delims=" %%1 in (host) do (
  3. set temp=%%1
  4. set temp=!temp:#=!
  5. echo !temp!
  6. )
复制代码

TOP

本帖最后由 wu26853767 于 2012-3-16 13:00 编辑

首先感谢楼上
  1. @echo off&&setlocal enabledelayedexpansion
  2. set datais=%SystemRoot%\system32\drivers\etc\
  3. if exist !datais!hosts_bak del !datais!hosts_bak/f/q
  4. for /f "tokens=*" %%a in (!datais!hosts) do (
  5. set var=%%a
  6. set "var=!var:#= !"
  7. >>!datais!hosts.txt echo !var!
  8. )
  9. ren !datais!hosts hosts_bak
  10. ren !datais!hosts.txt hosts
  11. echo 处理完毕。
  12. 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 处于打开状态。语句
请问还有其他办法删除井号并且保持内容不变吗?

TOP

1楼的代码删除第4行127.0.0.1前面的^符号,应该就可以了吧。

TOP

回复 4# apang
  1. @echo off
  2. set "hostfile=C:\WINDOWS\system32\drivers\etc\hosts"
  3. set "tempfile=%temp%\host.tmp"
  4. for /f "delims=:" %%a in ('findstr /n "127.0.0.1 ::1 localhost" "%hostfile%"') do (
  5.     set "num=%%a"
  6. )
  7. type nul >"%tempfile%"
  8. for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%hostfile%"') do (
  9.     set "str=%%b"
  10.     setlocal enabledelayedexpansion
  11.     if %%a leq %num% (
  12.         >>"%tempfile%" echo,%%b
  13.     ) else if "!str:~0,1!" equ "#" (
  14.         >>"%tempfile%" echo,!str:~1!
  15.     ) else (
  16.         >>"%tempfile%" echo,%%b
  17.     )
  18.     endlocal
  19. )
  20. move /y "%tempfile%" "%hostfile%"
复制代码
我发现这个样子修改之后windows7和windowsXP都可以通用了

TOP

返回列表