我想写入我的 Hosts 文件的路径。
我使用双引号来转义空格。
但我的输出将包含双引号。
这是我的代码:
setlocal EnableDelayedExpansion
CD C:\WINDOWS\System32\drivers\etc
@echo off
setlocal enabledelayedexpansion
set arrayline[0]="119.81.166.227 test.google.com.tw"
set arrayline[1]="119.81.166.227 test.yahoo.com.tw"
::read it using a FOR /L statement
for /l %%n in (0,1,1) do (
(
findstr /c:!arrayline[%%n]! hosts
IF ERRORLEVEL 0 echo !arrayline[%%n]!
)>> hosts
)
echo "complete"
pause
如果我像这样设置变量
set "arrayline[0]=119.81.166.227 test.google.com.tw"
或删除引号
set arrayline[1]=119.81.166.227 test.yahoo.com.tw
还会出现空格错误
如何正确设置主机文件中的文本?
最佳答案
尝试这样:
setlocal EnableDelayedExpansion
set "hosts=C:\WINDOWS\System32\drivers\etc\hosts"
@echo off
setlocal enabledelayedexpansion
set "arrayline[0]=119.81.166.227 test.google.com.tw"
set "arrayline[1]=119.81.166.227 test.yahoo.com.tw"
::read it using a FOR /L statement
for /l %%n in (0,1,1) do (
findstr /c:"!arrayline[%%n]!" "%hosts%" 1>nul 2>nul || (
(echo(!arrayline[%%n]!)>>"%hosts%"
)
)
echo "complete"
pause
关于batch-file - Windows 批处理 : write text to hosts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453311/