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主机文件中搜索字符串或数字并使用批处理脚本替换它们_Windows_Batch File - Fatal编程技术网

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

如何在windows主机文件中搜索字符串或数字并使用批处理脚本替换它们,windows,batch-file,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 我需要更多有关搜索和替

我一直在尝试创建一个批处理代码,以便在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%

不返回任何内容-空输出

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

if %errorlevel%==0 ren file newfile

可能重复的
findstr/x
意味着“完整的行确实完全匹配”,因此它将只提供仅包含
%domain%
的行。我想,你的意思是
/I
(忽略大小写),不要忘记以管理员身份运行脚本并禁用防病毒软件,因为它会阻止你写入(受保护的)主机文件。