1

How to assign %%parameter to variable?

FOR /F "tokens=1 delims= " %%A IN (connections.txt) DO (
   set USER=%%A

   echo A=%%A
   echo USER=%USER%
)

Output of this code:

A=user1
USER=

How to assign parameter %%A to variable USER?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Volodymyr Bezuglyy
  • 16,295
  • 33
  • 103
  • 133

1 Answers1

6

The parameter %%A was correctly assigned to USER variable, but USER value is not correctly shown.

To use the current value of a variable that was modified inside a FOR loop, you must use Delayed Variable Expansion, that is, change the percent by exclamation mark this way:

FOR /F "tokens=1 delims= " %%A IN (connections.txt) DO (
   set USER=%%A

   echo A=%%A
   echo USER=!USER!
)

and include this line at beginning:

setlocal EnableDelayedExpansion

Otherwise, the value of %USER% is the one the variable had before entering the FOR loop.

Aacini
  • 65,180
  • 12
  • 72
  • 108