Reputation: 435
I want write path to my Hosts file.
I use double quote to escape whitespace.
But my output will contain double quote.
Here is my code:
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
If I set variable like this
set "arrayline[0]=119.81.166.227 test.google.com.tw"
Or remove quote
set arrayline[1]=119.81.166.227 test.yahoo.com.tw
Also will get whitespace error
How to set text to my hosts file correct?
Upvotes: 2
Views: 137
Reputation: 57242
Try like this:
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
Upvotes: 4