Add
setlocal EnableDelayedExpansion
to the start of your script, then use
!errorlevel!
instead of %errorlevel%
Delayed Expansion will cause variables to be expanded at execution
time rather than at parse time~ http://ss64.com/nt/delayedexpansion.html
The answer to another question that pointed me in the right direction:
Errorlevel of command executed by batch for loop