1

I have this code working on my CMD promt (with one % instead %%) but when I executed it through a .bat file it isn't working.

for /f "tokens=5 delims=: " %%G in ('netsh interface IPv4 show dnsserver "Ethernet 2" ^| findstr /C:"Servidores DNS configurados estáticamente"') DO (Echo %%G)

I'm trying to do a .bat that swaps my main and secondary DNS for another two. I was trying with something like this:

@echo off  
cls  
color A
cls

SET DNS1=46.101.36.120
SET DNS2=46.101.149.135
SET DNSGoogle1=8.8.8.8
SET DNSGoogle2=8.8.4.4
SET NAME=Ethernet 2


for /f "tokens=5 delims=: " %%G in ('netsh interface IPv4 show dnsserver "%NAME%" ^| findstr /C:"Servidores DNS configurados estáticamente"') DO IF %DNSGoogle1%==%%G (goto netflixDNS) ELSE (goto googleDNS)


:netflixDNS
echo "Netflix DNS"
netsh interface ipv4 delete dnsserver "%NAME%" all
netsh interface ip set dnsservers "%NAME%" static %DNS1%
netsh interface ip add dnsservers "%NAME%" %DNS2% index=2
ipconfig /flushdns
PAUSE
EXIT

:googleDNS
echo "Google DNS"
netsh interface ipv4 delete dnsserver "%NAME%" all
netsh interface ip set dnsservers "%NAME%" static %DNSGoogle1%
netsh interface ip add dnsservers "%NAME%" %DNSGoogle2% index=2
ipconfig /flushdns
PAUSE
EXIT

For some reason my FORisn't working...

1
  • 2
    That's because of á character. I would simply shorten the text to Servidores DNS configurados est. Commented Nov 13, 2015 at 23:45

1 Answer 1

3

Although solved by wOxxOm's comment, this question requires some explanation: flagrant mojibake case. To avoid this kind of mojibake, run your batch script under the same code page which it's saved in: supposedly CP1252 if saved in ANSI encoding (notepad).

For proof:

Please do not insert next code snippet to a batch script. Copy & paste it in an open command line window:

chcp 850>NUL
echo áéíóúüýñäëêöã  850>ew-850.txt
chcp 1252>NUL
echo áéíóúüýñäëêöã 1252>ew1252.txt
for %G in (437 850 1252) do @chcp %G&>NUL timeout /T 1 &findstr "^" "ew*.txt"

Output:

==> chcp 850>NUL

==> echo áéíóúüýñäëêöã  850>ew-850.txt

==> chcp 1252>NUL

==> echo áéíóúüýñäëêöã 1252>ew1252.txt

==> for %G in (437 850 1252) do @chcp %G&>NUL timeout /T 1 &findstr "^" "ew*.txt"
Active code page: 437
ew-850.txt:áéíóúü∞ñäëêö╞  850
ew1252.txt:ßΘφ≤·ⁿ²±ΣδΩ÷π 1252
Active code page: 850
ew-850.txt:áéíóúüýñäëêöã  850
ew1252.txt:ßÚݾ·³²±õÙÛ÷Ò 1252
Active code page: 1252
ew-850.txt: ‚¡¢£ì¤„‰ˆ”Æ  850
ew1252.txt:áéíóúüýñäëêöã 1252

==>

Another example with (incomplete assortment of) Czech and Slovak accented letters and Central Europe encodings (code pages CP852 and CP1250):

chcp 852>NUL
echo áéíóúůýňěščřž  852>ee-852.txt
chcp 1250>NUL
echo áéíóúůýňěščřž 1250>ee1250.txt
for %G in (437 852 1250) do @chcp %G&>NUL timeout /T 1 &findstr "^" "ee*.txt"

Edit: Another extra illustrative example (copy&paste into a cmd window).

ECHO OFF
chcp 850>NUL
echo echo %~0 [áéíóúüýñäëêöã] 850>ew-850.bat
chcp 1252>NUL
echo echo %~0 [áéíóúüýñäëêöã]1252>ew1252.bat
chcp 850>NUL
ew-850.bat
ew1252.bat
chcp 1252>NUL
ew-850.bat
ew1252.bat
ECHO ON

Output:

==> ECHO OFF
chcp 850>NUL
echo echo %~0 [áéíóúüýñäëêöã] 850>ew-850.bat
chcp 1252>NUL
echo echo %~0 [áéíóúüýñäëêöã]1252>ew1252.bat
chcp 850>NUL
ew-850.bat
ew-850.bat [áéíóúüýñäëêöã] 850
ew1252.bat
ew1252.bat [ßÚݾ·³²±õÙÛ÷Ò]1252
chcp 1252>NUL
ew-850.bat
ew-850.bat [ ‚¡¢£ì¤„‰ˆ”Æ] 850
ew1252.bat
ew1252.bat [áéíóúüýñäëêöã]1252
ECHO ON

==>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.