如何在Windows主机文件中使用批处理脚本搜索字符串或数字并替换它们

时间:2014-09-05 06:40:24

标签: windows batch-file

我一直在尝试创建批处理代码,我可以在Windows主机文件C:\Windows\Systems32\drivers\etc\host中搜索IP和域名。

我已经创建了一个简单的搜索功能,如下所示:

@echo off
set hostpath=%WINDIR%\system32\drivers\etc\hosts

echo type the ip add or domain
set /p domain=
find "%domain%" %hostpath%

pause

我需要更多帮助来搜索和替换主机文件中找到的字符串,因为:

findstr /x /c:"%domain%" %hostpath%

不返回任何内容 - 空输出。

1 个答案:

答案 0 :(得分:0)

对于find命令,如果找到该文件,则%errorlevel%将为零(0)。所以你可以使用

if %errorlevel%==0 ren file newfile