A batch for wrapping long lines in text files

This is a handy batch file that does a simple thing, fix documents that have entire paragraphs on one line. Many of the utilities out there simply break lines at a particular column without regard to words, this batch/QBasic program only wraps lines between words.

The input file should be a normal text file and the output file must be a different name and valid. CON and PRN work for viewing and printing files but CON|MORE does not work (batches aren't redirectable).

The wrap position is fixed at column 77, edit the batch to change. It would be easy to alter the batch to take an extra parm for position, just change 'p=77' to 'p=%3' but don't forget to use it, probably should also change the initial idiot-checks. I prefer it fixed.

 
:: WORDWRAP.BAT - by Terry Newton
:: Usage: wordwrap infile.ext outfile.ext
:: Wraps long lines in text files, set to wrap at column 77,
:: change the 'p=77' part to wrap at a different position.
:: Uses QBASIC
@echo off
if .%2==. echo %0 infile outfile
if .%2==. goto end
if not exist %1 echo file not found
if not exist %1 goto end
echo >wwt$.bas :on error goto done
echo>>wwt$.bas open "%1" for input as #1:open "%2" for output as #2
echo>>wwt$.bas loopm:line input #1, a$:if a$="" then print #2,"":goto loopm
echo>>wwt$.bas loop0:p=77:if sgn(p-len(a$)+1)=1 then print #2,a$:goto loopm
echo>>wwt$.bas loop1:if mid$(a$,p,1)=" " then goto cut
echo>>wwt$.bas p=p-1:if p=0 then print #2,a$:goto loopm
echo>>wwt$.bas goto loop1
echo>>wwt$.bas cut:print #2,left$(a$,p-1):a$=right$(a$,len(a$)-p):goto loop0
echo>>wwt$.bas done:close #1:close #2:system
qbasic /run wwt$.bas
del wwt$.bas
:end