I'm not seeing a problem. When you have trouble with a script, it's often helpful to insert debug messages so you can see what the parameters are as of that line. In this case, with your error happening on this line:
write -l $+ $calc(%_i -1) $_doc(AutoID) $gettok($did(autos,8,%_i),5,32) $+ * $+ %g
Then insert a line above it like:
echo -s debug: $_doc(AutoID) $lines($_doc(AutoID)) //write -l $+ $calc(%_i -1) $_doc(AutoID) $gettok($did(autos,8,%_i),5,32) $+ * $+ %g
this lets you see how many lines were in the file at the time of the write attempt, and whether the filename existed. It shows the command that you can try repeating from the command line.
Something else to try:
I notice you're trying to mix /writeini and /write to the same filename. See if it helps if you place above the offending /write commands: