while $(%thinga $+ %xx,2) { inc %xx }

Ignoring everything else your construction of this is faulty

currently your trying to join the contents of %thinga with the contents of %xx, you then evaluate that twice (normally does nothing more than evaluates it once, since %thinga is normally $null)

what your wanting to do is creat a variable called %thinga%xx the italic section being the contents of %xx

while $($+(%,thinga,%xx),2) { inc %xx }

you must seperate the % from thinga to stop it being evaluated to its contents


PS: on quick observation JayTea's script seems a simple and cleaner method to go.