2014-11-03 31 views
1

我有其中的實體的主機文件地址:主機並希望檢查給定的實體是否出現在hosts文件中。所以我寫了: 在hosts文件:在Windows 7中使用批處理腳本檢查主機文件的實體?

# 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 

所以我想打印來自行查找,但沒有什麼不同的所有行偏偏讓我想知道是我的錯嗎?

回答

2

批量比較運算符==對空間敏感,必須擴展變量以進行比較。必須刪除==運算符周圍的空格,並且必須擴展變量或僅對變量名稱進行比較。

if not "%line%"=="%find%" (

這是您的腳本的主要問題。但是,我會建議使用find命令來執行此任務。

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

另外,如果我設置'SETLOCAL EnableDelayedExpansion'不工作,然後 '如果不是!行!==!找到! (「那麼一切都很好。 – Xelian 2014-11-04 09:25:11