Fixed setting variables in current session.
Inserted "endlocal" strangely in the loop but effectively to call the command first then set the variables outside of the batch file.