I had a problem with set not working in a batch file; it took a while to distil the problem; at first I thought it was to do with subroutine calls...
set
The sc
You need delayed expansion to be on, or the batch interpreter will interpolate all variables at parsing time, instead of run time.
setlocal enableextensions enabledelayedexpansion
See this question for an example and some great explanation of it.