ERASE
SET TALK OFF
SET ECHO OFF
SET BELL OFF
SET CONFIRM ON
SET DEFAULT TO A:
CLEAR
STORE 240 TO maxlength
*
* etsi muunnettavan tiedoston nimi*
*
STORE F TO ok
DO WHILE .NOT. ok
  STORE '        ' TO filename
@ 02,06 SAY 'Mink{ dBASE.DBF muunnat MailMerge.TXTiksi >> ';
  GET filename
  READ
  ERASE
  @ 04,06 SAY 'Varmistu, ett{ valittu .DBF-tiedosto on A:ssa! '
  SET CONSOLE OFF
  WAIT
  SET CONSOLE ON
  IF FILE('&filename')
    STORE T TO ok
  ELSE
    ERASE
    ? CHR(7)
    @ 01,00 'Levyll{ ovat ainoastaan n{m{ .DBF-tiedostot:'
  ?
  LIST FILES LIKE *.dbf
  STORE 100 TO delay
  DO WHILE delay > 0
    STORE delay-1 TO delay
  ENDDO delay > 0
  ERASE
  ENDIF FILE('&filename')
ENDDO .NOT. ok
*
* lue tiedoston rakenne ja valitse muunnettavat kent{t
*
ERASE
SET CONFIRM OFF
? ' *** Tutkitaan .DBF-tiedoston rakenne ***'
USE &filename
COPY STRUCTURE EXTENDED TO tempstru
USE tempstru
STORE F TO ok
DO WHILE .NOT. ok
  STORE 0 TO count
  STORE 2 TO offset
  STORE 0 TO length
  GOTO TOP
  ERASE
@ 01,01 SAY '  TIEDOSTO/TYYPPI/PITUUS';
  + ' - Ilmoita mitk{ kentist{ valitaan mukaan.'
  DO WHILE .NOT. eof
  STORE 'E' TO include
  @ # +(offset),01 SAY STR(#,2)+'.'+field:name+' '+field:type+' ';
      +STR(field:len,3)+' luetaan MailMerge-tiedostoksi (K/E)  ';
  GET include
  READ
  IF    !(include)='K'
   IF   field:type='C'
      STORE 'var'+STR(count+10,2) TO variable
      STORE field:name TO &variable
      STORE length+field:len TO length
      STORE count+ 1 TO count
   ENDIF field:type='C'
   IF   field:type='E'
      STORE 'var'+STR(count+10,2) TO variable
      STORE 'STR('+field:name;
        +','+STR(field:len,2);
        +','+STR(field:dec,2)+')' TO &variable
      STORE length+field:len TO length
      STORE COUNT+1 TO count
   ENDIF field:type='E'
ENDIF !(include)='K'
SKIP
IF # = 20
  ERASE
  STORE -19 TO offset
ENDIF # = 20	 
ENDDO .NOT. eof
@ 24,10 SAY 'Meniv{tk| valinnat t(iptop) vai f(ibaan) ';
GET ok
READ
IF length>maxlength
  STORE F TO ok
  ERASE
  ? CHR(7)
  ? 'MM-tietueesta tulee liian pitk{, ';
    + 'lyhenn{ '+STR(length-maxlength,3);
    + ' merkill{ kentti{!'
  STORE 100 TO DELAY
  DO WHILE delay>0
    STORE delay-1 TO delay
  ENDDO delay>0
  ERASE
  ENDIF length>maxlength
ENDDO .NOT. ok
USE
DELETE FILE tempstru
*
* saumaa kent{t yhdeksi v{liaikaiskent{ksi
*
ERASE
? ' ***  Rakennetaan v{liaikaistiedostoa ***'
?
USE template
COPY STRUCTURE TO tempfile
USE
ERASE
? ' ***  Tietueet saatetaan yhteen ***'
?
SELECT PRIMARY
USE &filename
SELECT SECONDARY
USE tempfile
SELECT PRIMARY
GOTO TOP
DO WHILE .NOT. eof .AND. count>0
  STORE '"' TO m:concat
  STORE 0 TO loop
  DO WHILE count>loop
    STORE 'var'+STR(loop+10,2) TO variable
    STORE &variable TO variable
    STORE m:concat-&variable TO m:concat		
    STORE loop+1 TO loop
    IF count>loop
      STORE m:concat-'","' TO m:concat
    ELSE
      STORE m:concat-'"' TO m:concat
    ENDIF count>loop
  ENDDO count>loop
  SELECT SECONDARY
  APPEND BLANK
  REPLACE concat WITH m:concat
  ? m:concat
  SELECT PRIMARY
  SKIP
  ENDDO .NOT. eof .AND. count>0
SELECT PRIMARY
USE
*
* kopioi saumattu .DBF-tiedosto .SDF/.MM-tiedostoksi
*
STORE F TO ok
DO WHILE .NOT. ok
  ERASE
  STORE "        " TO mmfilename
  SET CONFIRM ON
  @ 01,05 SAY 'Nime{ uusi MailMergen .TXT-tyypin tiedosto: ';
  GET mmfilename
  READ
  SET CONFIRM OFF
  @ 03,05 SAY 'Siis '+!(mmfilename)-'.TXT, T(osi) vai F(iba)? ' GET ok
  READ
ENDDO .NOT. ok
ERASE
@ 01,05 SAY !(mmfilename)-'.TXT rakentuu parhaillaan. Hetki vain!'
SELECT SECONDARY
COPY TO &mmfilename SDF DELIMITED WITH ,
USE 
DELETE FILE tempfile
ERASE
@ 05,05 SAY ' *** dBASE-tietosi ovat nyt my|s MailMerge-k{yt|ss{ *** '
@ 07,05 SAY '      (Tarkista vaikka allaolevasta hakemistosta!)'
SET INTENSITY OFF
LIST FILES LIKE *.txt
CLEAR
RELEASE ALL
QUIT
A:ssa! '
  SET CONSOLE OFF
  WAIT
  SET CONSOLE ON
  IF FILE('&filename')
    STORE T TO ok
  ELSE
    ERASE
    ? CHR(7)
    @ 0