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

[文本处理] 批处理怎么取消每行最后空格与输出空行?

有部分软件在使用过程中会修改host,我就写出这个还原host的批处理。
  1. @echo off&cls
  2. echo y|cacls %windir%\system32\drivers\etc\hosts /c /p administrators:f>nul
  3. echo y|cacls %windir%\system32\drivers\etc\hosts /e /c /p "power users:r" users:r system:f>nul
  4. attrib -h -r -s +a %windir%\system32\drivers\etc\hosts>nul
  5. cd.>%windir%\system32\drivers\etc\hosts
  6. echo # Copyright (c) 1993-1999 Microsoft Corp.>>%windir%\system32\drivers\etc\hosts
  7. echo #>>%windir%\system32\drivers\etc\hosts
  8. echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.>>%windir%\system32\drivers\etc\hosts
  9. echo #>>%windir%\system32\drivers\etc\hosts
  10. echo # This file contains the mappings of IP addresses to host names. Each>>%windir%\system32\drivers\etc\hosts
  11. echo # entry should be kept on an individual line. The IP address should>>%windir%\system32\drivers\etc\hosts
  12. echo # be placed in the first column followed by the corresponding host name.>>%windir%\system32\drivers\etc\hosts
  13. echo # The IP address and the host name should be separated by at least one>>%windir%\system32\drivers\etc\hosts
  14. echo # space.>>%windir%\system32\drivers\etc\hosts
  15. echo #>>%windir%\system32\drivers\etc\hosts
  16. echo # Additionally, comments (such as these) may be inserted on individual>>%windir%\system32\drivers\etc\hosts
  17. echo # lines or following the machine name denoted by a '#' symbol.>>%windir%\system32\drivers\etc\hosts
  18. echo #>>%windir%\system32\drivers\etc\hosts
  19. echo # For example:>>%windir%\system32\drivers\etc\hosts
  20. echo #>>%windir%\system32\drivers\etc\hosts
  21. echo #      102.54.94.97     rhino.acme.com          # source server>>%windir%\system32\drivers\etc\hosts
  22. echo #       38.25.63.10     x.acme.com              # x client host>>%windir%\system32\drivers\etc\hosts
  23. echo.>>%windir%\system32\drivers\etc\hosts
  24. echo 127.0.0.1       localhost>>%windir%\system32\drivers\etc\hosts
复制代码
运行后与系统原来host没有差别。
后来觉得这个批处理语句太多重复句子。
例如:“echo”与“%windir%\system32\drivers\etc\hosts”。
想精简批处理。

后来用for尝试。
  1. @echo off
  2. cls
  3. echo y|cacls %windir%\system32\drivers\etc\hosts /c /p administrators:f>nul
  4. echo y|cacls %windir%\system32\drivers\etc\hosts /e /c /p "power users:r" users:r system:f>nul
  5. attrib -h -r -s +a %windir%\system32\drivers\etc\hosts>nul
  6. cd.>%windir%\system32\drivers\etc\hosts
  7. for %%1 in (
  8. "# Copyright (c) 1993-1999 Microsoft Corp."
  9. "#"
  10. "# This is a sample HOSTS file used by Microsoft TCP/IP for Windows."
  11. "#"
  12. "# This file contains the mappings of IP addresses to host names. Each"
  13. "# entry should be kept on an individual line. The IP address should"
  14. "# be placed in the first column followed by the corresponding host name."
  15. "# The IP address and the host name should be separated by at least one"
  16. "# space."
  17. "#"
  18. "# Additionally, comments (such as these) may be inserted on individual"
  19. "# lines or following the machine name denoted by a "#" symbol."
  20. "#"
  21. "# For example:"
  22. "#"
  23. "#      102.54.94.97     rhino.acme.com          # source server"
  24. "#       38.25.63.10     x.acme.com              # x client host"
  25. ""
  26. "127.0.0.1       localhost"
  27. ) do echo %%~1 >>%windir%\system32\drivers\etc\hosts
  28. pause
复制代码
但结果是每行最后都有一个空格,而且不能输出空行。(“” host结果显示 ECHO 处于关闭状态。)

也许可以这样,第一次运行为备份,之后运行均为还原:
  1. @echo off
  2. (
  3.    echo @echo off
  4.    echo more^>host +3 %%0
  5.    echo exit
  6.    type host
  7. )>%0
复制代码

TOP

首先感谢zm900612 的回复。
如原文件已被修改,备份也无用,更别说还原。

但我想要运行的结果就是第一个批处理效果。
第二个只是自己参考“echo多行输出”修改。
但发现每行最后都有1个空格,还有不会如何输出空行。

某些软件每次运行时会自动修改host内容,还有就是权限也被修改拒绝。
导致用户不能修改host与部分网站不能正常访问。
为了解决这个问题才弄此批处理、

我目的只是想优化一下第一个批处理。(使用后与系统内容默认一致,含权限)
内容精简类似第二个批处理。(使用后每行尾部自动有空格还有不会如何输出空行)
这样批处理看起来起码简洁一点。
只是不太清除如何处理。

TOP

  1. echo,%%~1>>%windir%\system32\drivers\etc\hosts
复制代码

TOP

#开头的内容可以全部无视掉,都为注释。

TOP

什么叫原文件被修改就不能还原?
你把没被修改的原始HOSTS或者正确的HOSTS备份一个不就可以了?
根本不需要FOR,ECHO。
修改权限后,直接COPY 替换就行了。

TOP

你把下面的保存为hosts.txt,复制你的,引号自己去掉。
  1. "# Copyright (c) 1993-1999 Microsoft Corp."
  2. "#"
  3. "# This is a sample HOSTS file used by Microsoft TCP/IP for Windows."
  4. "#"
  5. "# This file contains the mappings of IP addresses to host names. Each"
  6. "# entry should be kept on an individual line. The IP address should"
  7. "# be placed in the first column followed by the corresponding host name."
  8. "# The IP address and the host name should be separated by at least one"
  9. "# space."
  10. "#"
  11. "# Additionally, comments (such as these) may be inserted on individual"
  12. "# lines or following the machine name denoted by a "#" symbol."
  13. "#"
  14. "# For example:"
  15. "#"
  16. "#      102.54.94.97     rhino.acme.com          # source server"
  17. "#       38.25.63.10     x.acme.com              # x client host"
  18. ""
  19. "127.0.0.1       localhost"
复制代码
然后
  1. cacls ......增加修改权限.......
  2. type hosts.txt>hosts
  3. cacls .......去掉修改权限......
复制代码

TOP

返回列表