Daniella
Daniella

Reputation: 181

Batch File Setting an Environment Variable

I currently have this code in my batch file to store a value in variable %%i:

FOR /F %%i in (AylaUnits.txt) DO set %%i 
set "$Unit=%%i"
echo The unit is : %$Unit%
Pause

This seems to loop fine, but for each item I get this response:

set AC000W000004591
Environment variable AC000W000004595 not defined
The unit is : %i

There is going to be 100+ different lines in the text file. Will I need to create an environmental variables for each one? I don't think that's the best way to do it and I'm not sure I'm understanding environmental variables correctly.

I need to plug each line of the file into a url section if that's of any importance.

Thanks, DM

Upvotes: 0

Views: 206

Answers (1)

Alex
Alex

Reputation: 917

You have a syntax issue, see the proposed change below:

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F %%i in (AylaUnits.txt) DO (
    set "$Unit=%%i"
    echo The unit is : !$Unit!
)
Pause

Please see the edit above, since we're setting the variable inside the for loop, we need to use SETLOCAL ENABLEDELAYEDEXPANSION to be able to expand on it. Since we're expanding the var that's set in the for loop, we need to use ! instead of % when using those variables.

Upvotes: 1

Related Questions