|it looks like it should work, except you didn't reset "n" to zero. i think you should set n to zero here:|
set /a i-=1
:: here, reset var. "n" to zero
set /a n=0
for /f "tokens=* delims= " %%a in (%name%) do (
other than that, it should work, looks like to me. maybe if you post what it is or isn't doing, and errormessages if any.
also, if values aren't coming out right, place echos and pauses at "strategic" points to see what's going on. H.t.h.