As part of a task that I am working on for a customer, I need to make some modifications to the enterprise logon script. Windows command-line is definitely not my area of expertise, but I am doing OK with most of it. However, I am running into the following inconsistent behavior, that I cannot figure out.

The following commands:

Set TMP=
For /f "tokens=1,2,3 delims=:" %A in ('ipconfig ^| Find "Default Gateway" ^| Findstr/N "."') do (If not defined TMP  Set TMP=%~C)

When executed at the command prompt (a windows 7 CMD.EXE window), work as expected and TMP gets set with the IP address of my default gateway. However, if I execute the exact same commands from a CMD file, in the same window, it fails with the following output (Echo On):

>Set TMP=
~C) was unexpected at this time.

>For /f "tokens=1,2,3 delims=:" ~C)

I have tried both CALL and running the script directly. I have tried it both with and without usebackq, and with and without the parenthesis after the DO, with the same results every time: it works interactively, but not from a script in the same session.

What the heck is going on here and how can I fix it?


Here is a part of help for cmd output:

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.