备份主机文件的批处理脚本

Batch script for backup hosts file

我正在编写一个执行备份的批处理脚本。

需要复制 "hosts" 文件,条件如下: “如果系统 "hosts" 文件包含任何未注释的条目,则复制它”。

有什么想法吗?

作为注释行以主机文件中的 "#" 字符串开头,因此, 你应该使用 Find /V "#" 来显示所有不包含指定字符串的行 "#"

有关 Find /?

的更多帮助

你可以这样做:

@echo off
Rem Batch script to copy uncommented entries of your hosts file
set "BackupHostsFile=%userprofile%\Desktop\BackupHostsFile.txt"
If Exist "%BackupHostsFile%" Del "%BackupHostsFile%"
set "hostspath=%windir%\System32\drivers\etc\hosts"
Rem Find /V "#" : To display all lines NOT containing the specified string "#"
for /f "delims=" %%a in ('Type "%hostspath%" ^| find /v "#"') Do (
    If Not %%a==""  echo %%a >> "%BackupHostsFile%"
)
Start "" "%BackupHostsFile%"