Update: this was not the case. The problem was with .bat language quirk: if you change an environment variable inside an if () block, the value you read back from it won't change until after that if () block.