Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.
share|improve this answer
    
Yep that was it! Any idea why the extra "%" is necessary in command files? –  RBarryYoung Jan 15 '13 at 22:02
    
It's comes from Windows Command interpreter origins (MS DOS). Seems support.microsoft.com/kb/75634/en-us reveals it. –  Sergey Jan 16 '13 at 6:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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