batch-file - Windows 批处理 : write text to hosts

标签 batch-file

我想写入我的 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/

相关文章:

path - 在批处理文件中创建相对路径

latex - 在 Windows 中编译多个 LaTeX 文件

json - 如何批量递增文件名?

batch-file - 如何将变量的值写入 dos 文本文件?

batch-file - 命名参数如何传递给批处理文件?

BAT文件中的SQLCMD实用程序-如何在语法错误的情况下返回ERRORLEVEL

windows - 是否有用于 UNIX 的 "Windows Batch file"模拟器/解释器?

windows-vista - 如何在Windows Vista上从批处理文件检索文件的版本?

python - 如何在 Windows 下从命令行打开文件进行编辑?

java - .BAT 文件 : Launch the JVM in background