Title: Why doesn't environment variable get updated in script
Tags: windows;cmd
Question: I am basically a Linux guy forced into a Windows world lately, so I need to write a bat script, but I ran into the following problem.

Here is my .bat script

```///////////////////////////
echo.
echo This is testbat script
echo -----------------------------------------------------------
echo.  

if "%1"=="" (
    echo "You did not enter an argument
) else (

    set "myvar="
    echo Argument is %1% 
    set myvar=%1%
    if "%myvar%"=="%1%" (
        echo myvar is %myvar%
    ) else (
        echo myvar is not set to %1
    )   
)
////////////////////////////////////////////////////////
```

It seems that I need to run this script twice to get myvar to change.

For example, 
FIRST RUN:

```
  testbat.bat hello
```

OUTPUT:
          This is testbat script
          -----------------------

```      Argument is hello
      myvar is not set to hello
```

SECOND RUN:

```
  testbat.bat hello
```

OUTPUT:
          This is testbat script
          -----------------------

```      Argument is hello
      myvar is hello
```

NOW CHANGE the argument to bye
THIRD RUN:

```
  testbat.bat bye
```

OUTPUT:
          This is testbat script
          -----------------------

```      Argument is bye
      myvar is not set to bye   (In fact, it is still hello here)
```

FOURTH RUN (same input as THIRD):

```     &gt; testbat.bat bye
```

OUTPUT:
          This is testbat script
          -----------------------

```      Argument is bye
      myvar is bye    (Finally gets updated)
```

////////////////////////////////////

My question is why the script doesn't update the environment variable the first time?
Why do I need to run the script a second time to get the variable to change to the new value in the script?   I used the SET command and discovered that the value is changed in the environment, why does the script output reflect the old value.  Of course, the value in the environment might not change until after the script completed, not sure.

I'm running the script and then using the up arrow to edit the command line if that makes any difference, it doesn't seem to though. 

Comment: Seems as if it takes some finite amount of time for the SET to take effect.

Comment: No, it's a one shot that takes a directory path as input and calculates the CRC of each file in that directory and creates (or updates) an existing file in that directory with the CRC information.

Comment: Are you running your script inside a loop?

Here is the accepted answer: You cannot use ```%1%``` as an environment variable because ```%1``` is a command line replaceable parameter.

To ```set/change``` and ```display``` a variable within parentheses or a loop you need

```@echo off
setlocal enabledelayedexpansion
```

and use ```echo !myvar!```

Comment for this answer: If I replace %1% with %1 I still have the same problem.
Comment for this answer: I'm not in a loop, but I think you are pointing me in the right direction. Any complicated construct, such as my if/else would result in expansion before execution.  

I modified the script to use setlocal enabledelayedexpansion and use !myvar! to reference the variable while local is active. After these checks, I am out of the if/else, I do a endlocal, and my variable is set appropriately thereafter. 
Thanks
Comment for this answer: See the second part of my answer


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:07/14/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<windows><cmd>', 'Title': "Why doesn't environment variable get updated in script", 'ViewCount': 73, 'id': 24745836, 'user_AboutMe': "<p>Sr Software Systems Engineer with over 20 years solving problems, implementing solutions, and jamming out. </p>\n\n<p>B.S Applied Physics from Georgia Tech.  Why Applied Physics, because I wanted to know everything about everything, and still do.  </p>\n\n<p>That's why I have done many diverse things: satellite orbit determination, computer engineering, software engineering, building my own computers, teaching myself guitar and singing so I could bring the songs in my head to life!</p>\n", 'user_AccountId': 1912298.0, 'user_DisplayName': 'user1725779', 'user_Reputation': 138.0, 'user_Views': 28.0, 'user_id': 1725779.0}}