BuvinJ
BuvinJ

Reputation: 11046

Batch / cmd for loop syntax mistake

This is not the full scope of what I'm doing. I've distilled down... I'm sure there is something tiny and stupid I'm failing to account for here, I just don't see it.

Can someone please tell me why I can execute the following netsh command successfully:

netsh wlan show profile name="SomeWifi"

Yet, it fails in a for loop?

for /F "tokens=1,2 delims=:" %a in ('netsh wlan show profile name="SomeWifi"') do echo %a

Instead of the profile info, in the for loop it spits out this error message:

There is no such wireless interface on the system.

What am I missing? Is the context changed in the parenthesis (like the user)? Is there a character escape issue?

Upvotes: 1

Views: 291

Answers (1)

MC ND
MC ND

Reputation: 70923

There are some characters that need to be escaped (using ^ as a prefix) when directly used in a for /f command.

Some of them are the usual & and | that having a special meaning to the parser seem that obviously need scaping. Another problematic character is the closing parenthesis ()) that can be seen as the closing parenthesis of the in clause of the for command.

But some characters (ex. ,, ;) need escaping just because they are seen as delimiters and removed. In your case = generates the problem

You can use

for /F "tokens=1,2 delims=:" %a in ('netsh wlan show profile name^="SomeWifi"') do echo %a

Upvotes: 4

Related Questions