Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否在Windows 7中使用批处理脚本检查主机文件中的实体?_Windows_Batch File_Scripting_Hosts - Fatal编程技术网

是否在Windows 7中使用批处理脚本检查主机文件中的实体?

是否在Windows 7中使用批处理脚本检查主机文件中的实体?,windows,batch-file,scripting,hosts,Windows,Batch File,Scripting,Hosts,我有一个主机文件,其中包含实体地址:hosts,我想检查主机文件中是否存在给定的实体。于是我写道: 在主机文件中: # Copyright (c) 1993-2009 Microsoft Corp. 129.0.2.2 tralala.com 在我的批处理脚本中,我写道: @if "%DEBUG%" == "" @echo off @rem ############################################ @rem # Remove host from windows

我有一个主机文件,其中包含实体地址:hosts,我想检查主机文件中是否存在给定的实体。于是我写道: 在主机文件中:

# Copyright (c) 1993-2009 Microsoft Corp.
129.0.2.2 tralala.com
在我的批处理脚本中,我写道:

@if "%DEBUG%" == "" @echo off

@rem ############################################
@rem # Remove host from windows hosts file      #
@rem ############################################

if "%OS%"=="Windows_NT" setlocal

:start
set "hostpath=%systemroot%\system32\drivers\etc\hosts"
goto addFindIPAddress

:addFindIPAddress
@rem set the string you wish to find
set find="129.0.2.2 tralala.com"
goto checkHosts

:checkHosts
for /f "tokens=*" %%a in (%hostpath%) do call :processline %%a
goto :mainEnd

:processline
set line=%*
if NOT line == find (
echo %line%
)
goto :eof

:mainEnd
if "%OS%"=="Windows_NT" endlocal
PAUSE

因此,我想打印所有与查找行不同的行,但什么也没有发生,所以我想知道是我错了吗?

批比较运算符
==
对空间敏感,必须展开变量进行比较。
必须删除
=
运算符周围的空格,并且必须删除变量展开,或者只比较变量名

if not "%line%"=="%find%" (
这是脚本的主要问题。但是,我建议使用
find
命令来执行此任务

find /v "129.0.2.2 tralala.com" "%systemroot%\system32\drivers\etc\hosts"

如果我设置了
setlocalenabledelayedexpansion
,然后设置了
则不工作!行!==!找到!(
然后一切正常。