Search code examples
windowsbatch-filecommand-linedos

Difference between use of %%A and %A%


I have the following script:

FOR /F "tokens=1-4" %%A IN ("REG QUERY HKCU\Environment\") DO (
    ECHO %%A
)

However if we change ECHO %%A to:

ECHO %A%

The output is the same.

Normally in batch scripts I echo my variables with %A%, what is the difference and why do people sometimes use %%A as appose to %A%?


Solution

  • Two cases, two reasons:

    • The doubled % is the documented way to use a temporary variable, e.g., for the for loop, in a batch file (not on the command-line).
    • Putting % on each side of a variable is documented as the way to expand an environment variable.

    Presumably your loop just happens to work because the temporary variable is visible in the same tables as environment variables - but like variables within the scope of setlocal, it goes away.

    MSDN documents both in kb75634, Percent Signs Stripped from Batch File Text