标题: [文本处理] 批处理怎么取消每行最后空格与输出空行? [打印本页]
作者: yuelaner 时间: 2011-3-21 20:05 标题: 批处理怎么取消每行最后空格与输出空行?
有部分软件在使用过程中会修改host,我就写出这个还原host的批处理。- @echo off&cls
- echo y|cacls %windir%\system32\drivers\etc\hosts /c /p administrators:f>nul
- echo y|cacls %windir%\system32\drivers\etc\hosts /e /c /p "power users:r" users:r system:f>nul
- attrib -h -r -s +a %windir%\system32\drivers\etc\hosts>nul
- cd.>%windir%\system32\drivers\etc\hosts
- echo # Copyright (c) 1993-1999 Microsoft Corp.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # This file contains the mappings of IP addresses to host names. Each>>%windir%\system32\drivers\etc\hosts
- echo # entry should be kept on an individual line. The IP address should>>%windir%\system32\drivers\etc\hosts
- echo # be placed in the first column followed by the corresponding host name.>>%windir%\system32\drivers\etc\hosts
- echo # The IP address and the host name should be separated by at least one>>%windir%\system32\drivers\etc\hosts
- echo # space.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # Additionally, comments (such as these) may be inserted on individual>>%windir%\system32\drivers\etc\hosts
- echo # lines or following the machine name denoted by a '#' symbol.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # For example:>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # 102.54.94.97 rhino.acme.com # source server>>%windir%\system32\drivers\etc\hosts
- echo # 38.25.63.10 x.acme.com # x client host>>%windir%\system32\drivers\etc\hosts
- echo.>>%windir%\system32\drivers\etc\hosts
- echo 127.0.0.1 localhost>>%windir%\system32\drivers\etc\hosts
复制代码
运行后与系统原来host没有差别。
后来觉得这个批处理语句太多重复句子。
例如:“echo”与“%windir%\system32\drivers\etc\hosts”。
想精简批处理。
后来用for尝试。- @echo off
- cls
- echo y|cacls %windir%\system32\drivers\etc\hosts /c /p administrators:f>nul
- echo y|cacls %windir%\system32\drivers\etc\hosts /e /c /p "power users:r" users:r system:f>nul
- attrib -h -r -s +a %windir%\system32\drivers\etc\hosts>nul
- cd.>%windir%\system32\drivers\etc\hosts
- for %%1 in (
- "# 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"
- ""
- "127.0.0.1 localhost"
- ) do echo %%~1 >>%windir%\system32\drivers\etc\hosts
- pause
复制代码
但结果是每行最后都有一个空格,而且不能输出空行。(“” host结果显示 ECHO 处于关闭状态。)
作者: CrLf 时间: 2011-3-21 20:13
也许可以这样,第一次运行为备份,之后运行均为还原:- @echo off
- (
- echo @echo off
- echo more^>host +3 %%0
- echo exit
- type host
- )>%0
复制代码
作者: yuelaner 时间: 2011-3-22 07:54
首先感谢zm900612 的回复。
如原文件已被修改,备份也无用,更别说还原。
但我想要运行的结果就是第一个批处理效果。
第二个只是自己参考“echo多行输出”修改。
但发现每行最后都有1个空格,还有不会如何输出空行。
某些软件每次运行时会自动修改host内容,还有就是权限也被修改拒绝。
导致用户不能修改host与部分网站不能正常访问。
为了解决这个问题才弄此批处理、
我目的只是想优化一下第一个批处理。(使用后与系统内容默认一致,含权限)
内容精简类似第二个批处理。(使用后每行尾部自动有空格还有不会如何输出空行)
这样批处理看起来起码简洁一点。
只是不太清除如何处理。
作者: tmplinshi 时间: 2011-3-24 03:35
- echo,%%~1>>%windir%\system32\drivers\etc\hosts
复制代码
作者: TX_kakashi 时间: 2011-3-24 03:44
#开头的内容可以全部无视掉,都为注释。
作者: caruko 时间: 2011-3-24 21:15
什么叫原文件被修改就不能还原?
你把没被修改的原始HOSTS或者正确的HOSTS备份一个不就可以了?
根本不需要FOR,ECHO。
修改权限后,直接COPY 替换就行了。
作者: caruko 时间: 2011-3-24 21:19
你把下面的保存为hosts.txt,复制你的,引号自己去掉。- "# 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"
-
- ""
-
- "127.0.0.1 localhost"
复制代码
然后- cacls ......增加修改权限.......
- type hosts.txt>hosts
- cacls .......去掉修改权限......
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |