|Seems the IF command is inside parenthesis so you could/should use delayed expansion e.g. Setlocal enabledelayedexpansion|
Enter Set /? at the Command Prompt for documentation
You could also try canceling the environment variable by setting it to blank prior to the second and subsequent execution of the script.
Please come back & tell us if your problem is resolved.