我正在尝试编写一个批处理文件来查找和替换主机文件中的 IP 地址。
我做了一些研究并发现了这一点,但它似乎不起作用。我得到了“完成”的最后回声。但它不起作用。
@echo off
REM Set a variable for the Windows hosts file location
set hostpath=%systemroot%\system32\drivers\etc
set hostfile=hosts
REM Make the hosts file writable
attrib -r %hostpath%\%hostfile%
setlocal enabledelayedexpansion
set string=%hostpath%\%hostfile%
REM set the string you wish to find
set find=OLD IP
REM set the string you wish to replace with
set replace=NEW IP
call set string=%%string:!find!=!replace!%%
echo %string%
REM Make the hosts file un-writable
attrib +r %hostpath%\%hostfile%
echo Done.