10 CLEAR
20 PRINT CHR$(26)
30 PRINT"    REFORMAT.BAS by Gobind Singh Khalsa":PRINT
40 PRINT"Is a program to reformat disk data files
50 PRINT"    more to your liking..."
60 PRINT
70 PRINT"It is assumed that:"
80 PRINT"    Records are separated by a CR/LF combination, and"
90 PRINT"    fields within each record are delimited by commas"
100 PRINT"    and there are the same number of fields in each
110 PRINT"    record.  "
120 PRINT"Things get done in the following order:"
130 PRINT"    Drop preceeding & trailing"
140 PRINT"    Find & Replace"
150 PRINT"    Add preceeding & trailing"
160 PRINT"    Adjust field length & truncate"
170 PRINT
180 PRINT"  Find = <CR> will find all characters"
190 PRINT"  Replace = DeletE will delete the field"
200 PRINT"  Add Preceeding or Trailing = ";CHR$(34);",";CHR$(34);" will add a field"
210 PRINT:PRINT
220 INPUT"Hit <CR> to continue ";N
230 PRINT CHR$(26)
240 A$="":B$="":C$="":D$="":E$="":F$="":G$="":H$="":I$="":J$="":Z$="":Q$=""
250 DD$="":FF$="":LL$="N":L$="":MM$="":OK$="NNN"
260 M=0:MM=0
270 PRINT"What is the full name of the file you wish
280 INPUT"to re-format ";A$:PRINT
290 INPUT"How many fields per record ";NN
300 INPUT"Modify only field # ";M
310 M=ABS(M):IF M>NN THEN GOTO 290
320 IF M<1 THEN GOTO 370
330 INPUT"Only if field # ";MM
340 MM=ABS(MM):IF MM>M THEN GOTO 290
350 IF MM<1 THEN GOTO 370
360 INPUT"Contains string = ";MM$
370 PRINT"What name do you wish to give to the resulting
380 INPUT"output file ";B$:PRINT
390 IF B$=A$ THEN GOTO 270
400 PRINT"Do you wish to drop:"
410 INPUT"Preceeding (Y/N) ";C$
420 IF (LEFT$(C$,1)="Y") OR (LEFT$(C$,1)="y")  THEN INPUT"     Character (blanks are assumed) ";D$:IF D$="" THEN D$=" "
430 INPUT"Trailing (Y/N) ";E$
440 IF (LEFT$(E$,1)="Y") OR (LEFT$(E$,1)="y") THEN INPUT"     Character (blanks are assumed) ";F$:IF F$="" THEN F$=" "
450 PRINT"Do you wish to add:"
460 INPUT"Preceeding (Y/N) ";CC$
470 IF (LEFT$(CC$,1)="Y") OR (LEFT$(CC$,1)="y") THEN INPUT"     String = ";DD$
480 INPUT"Trailing (Y/N) ";EE$
490 IF (LEFT$(EE$,1)="Y") OR (LEFT$(EE$,1)="y") THEN INPUT"     String = ";FF$
500 INPUT"Find & Replace (Y/N) ";G$
510 IF (LEFT$(G$,1)="Y") OR (LEFT$(G$,1)="y")  THEN INPUT"Find = ";H$:INPUT"Replace = ";I$:PRINT "If a blank is specified, it must ":PRINT"be bounded by quotes (i.e. ";CHR$(34);" ";CHR$(34);" )"
520 INPUT"Specify a length for each field (Y/N) ";LL$
530 IF (LEFT$(LL$,1)="Y") OR (LEFT$(LL$,1)="y") THEN INPUT"     Field length = ";L:INPUT"     Justified (L)eft or (R)ight ";L$:IF (LEFT$(L$,1)<>"R") AND (LEFT$(L$,1)<>"r") THEN L$="L"
540 PRINT
550 INPUT"All entries correct (Y/N) ";J$
560 PRINT CHR$(26)
570 IF (LEFT$(J$,1)="N") OR (LEFT$(J$,1)="n")  THEN GOTO 270
580 PRINT:PRINT:PRINT:PRINT:PRINT"     Please don't interrupt until I'm done..."
590 OPEN "I",#1,A$
600 OPEN "O",#2,B$
610 REM INPUT SECTION
620 YY=1:OK$="YYY":M1$="N":M2$="N":M3$="N"
630 INPUT#1,Z$:IF EOF(1) THEN GOTO 790
640 IF M=0 GOTO 660   ' No selective field processing
650 GOSUB 1160  ' See if this is the selected field
660 IF OK$<>"YYY" THEN GOTO 740  ' If not, skip
670 REM PROCESSING SECTION
680 IF (LEFT$(C$,1)="Y") OR (LEFT$(C$,1)="y") THEN GOSUB 840  'Preceeding
690 IF (LEFT$(E$,1)="Y") OR (LEFT$(E$,1)="y") THEN GOSUB 870  'Trailing
700 IF (LEFT$(G$,1)="Y") OR (LEFT$(E$,1)="y") THEN GOSUB 900  'Find & Replace
710 Z$=DD$+Z$+FF$  ' Add preceeding & trailing
720 IF (LEFT$(LL$,1)="Y") OR (LEFT$(LL$,1)="y") THEN GOSUB 1040  'Adjust field length
730 REM OUTPUT SECTION
740 IF NOT (I$="DeletE" AND OK$="YYY") THEN Q$=Q$+","+Z$  ' Connect the fields to form a record
750 YY=YY+1:IF YY<(NN+1) THEN GOTO 630  ' If need more fields
760 Q$=RIGHT$(Q$,LEN(Q$)-1)
770 PRINT#2,Q$:Q$=""
780 GOTO 620
790 CLOSE
800 PRINT CHR$(26):PRINT:PRINT:PRINT:PRINT
810 PRINT"          I'm done !"
820 END
830 REM SUBROUTINES
840 REM 'Preceeding' subroutine
850 WHILE LEFT$(Z$,LEN(D$))=D$:Z$=RIGHT$(Z$,LEN(Z$)-LEN(D$)):WEND
860 RETURN
870 REM 'Trailing' subroutine
880 WHILE RIGHT$(Z$,LEN(F$))=F$:Z$=LEFT$(Z$,LEN(Z$)-LEN(F$)):WEND
890 RETURN
900 REM 'Find' subroutine
910 IF I$="DeletE" THEN Z$="":GOTO 970
920 IF H$="" THEN GOTO 960
930 FOR X=1 TO LEN(Z$)-LEN(H$)+1
940 IF MID$(Z$,X,LEN(H$))=H$ THEN GOSUB 980
950 NEXT X:GOTO 970
960 FOR X=1 TO LEN(Z$):GOSUB 980:NEXT X
970 RETURN
980 REM 'Replace' subroutine
990 Z1$=LEFT$(Z$,X-1)
1000 Z2$=RIGHT$(Z$,LEN(Z$)-LEN(H$)-X+1)
1010 Z3$=Z1$+I$+Z2$
1020 IF H$="" THEN Z$=LEFT$(Z3$,LEN(Z$)) ELSE Z$=Z3$
1030 RETURN
1040 REM Adjust field length subroutine
1050 IF L$="L" THEN GOSUB 1080
1060 IF L$<>"L" THEN GOSUB 1120
1070 RETURN
1080 REM Justify left subroutine
1090 WHILE LEN(Z$)<L:Z$=Z$+" ":WEND
1100 Z$=LEFT$(Z$,L)
1110 RETURN
1120 REM Justify right subroutine
1130 WHILE LEN(Z$)<L:Z$=" "+Z$:WEND
1140 Z$=RIGHT$(Z$,L)
1150 RETURN
1160 REM Is this the field to change?
1170 IF M=YY THEN M1$="Y" ELSE M1$="N"
1180 IF MM=YY THEN M2$="Y"
1190 IF MM=0 THEN M2$="Y"
1200 FOR X=1 TO LEN(Z$)-LEN(MM$)+1
1210 IF MID$(Z$,X,LEN(MM$))=MM$ THEN M3$="Y"
1220 NEXT X
1230 IF MM$="" THEN M3$="Y"
1240 OK$=M1$+M2$+M3$
1250 RETURN
+1
1210 IF MID$(Z$,X,LEN(MM$))=MM$ THEN M3$="Y"
1220 NEXT X
1230 IF MM$="" 