home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / library / dbase / duplicat / moddbf.prg < prev   
Text File  |  1987-02-26  |  8KB  |  235 lines

  1. ** MODDBF.PRG - utility for creating and editting text files from which to
  2. **              create new or modified structures from existing datafiles
  3. **              copyright Steve Titterud 1987
  4. close data
  5. set status off
  6. set scoreboard off
  7. set menu off
  8. set talk off
  9. set safety off
  10. set echo off
  11. set heading off
  12. clear
  13. text  && delete this help text if desired or when no longer needed...
  14.  
  15.      The MODIFY STRUCTURE command is a rather unwieldy way to change a
  16.      datafile's structure, if the changes are complex: e.g., rearranging
  17.      field order when there are a large number of fields.  There are other
  18.      methods more convenient in some cases.  This is a utility I have used,
  19.      and maybe it might save you some work, too.
  20.  
  21.      The following program expects input of a source datafile name and a
  22.      target datafile name.  The source file is a datafile whose structure
  23.      you intend to modify.  The target file will be a structure extended file
  24.      which may be used to generate the newly structured file with the
  25.      CREATE FROM commmand, after its data is modified.
  26.  
  27.      First, we create the structure file from the source, then, USEing it,
  28.      write a text file with the COPY TO...SDF command, then edit this SDF file
  29.      with your editor.  Then, with any blank (zapped) structure file in use,
  30.      APPEND FROM...SDF the editted text file.  The last steps will then
  31.      be to rename the original datafile (as a backup for source), CREATE FROM
  32.      the newly formed structure file, and lastly, append from the renamed
  33.      source file into the new file.  By the way, you can do the same process
  34.      with a structure extended file as the source!!
  35.  
  36.      This sounds complicated, but in practice is quite simple - try it!
  37. endtext
  38. wait
  39. clear
  40. text
  41.      Some conventions assumed here:
  42.  
  43.           - pressing <Esc> during input will abort the program; there is
  44.             little error-checking in the program; modify to suit your needs
  45.  
  46.           - if you do not give a file extension, .DBF will be supplied for
  47.             the source file, .EXT will be supplied for the structure file.
  48.  
  49.           - the text file output in the COPY TO...SDF command is given 
  50.             the same filename as the source, but for the extension .SDF.
  51.  
  52.           - in the final append operation (if chosen) from the original
  53.             source file to the new structure, the source file is renamed
  54.             by changing its extension to .OLD - the newly structured file
  55.             takes the same name as the source had at the beginning
  56.  
  57.           - if you choose to NOT append from the source into the altered
  58.             structure, the original source file is left unaltered
  59.  
  60.           - the MODI COMM command is used to call the editor; if you don't
  61.             have an editor specified in CONFIG.DB, you will get the dBase
  62.             editor; keep edits in same columnar format as in the SDF file, else
  63.             you may get unpredictable results when creating and/or appending
  64. endtext
  65. wait
  66. clear
  67. changeit=.T.
  68. quitter=.F.
  69. do while .not. quitter
  70.    clear
  71.    source=space(12)
  72.    target=space(12)
  73.    @ 10,15 say "What file to modify?" get source
  74.    @ 12,15 say "(Default extension of .DBF supplied if none given.)"
  75.    read
  76.    r=readkey()
  77.    if r=12 .or. r=268
  78.       close data
  79.       return
  80.    endif
  81.    source=upper(ltrim(rtrim(source)))
  82.    if .not. "."$source
  83.       source=source+".DBF"
  84.    else
  85.       if right(source,1)="."
  86.          source=source+"DBF"
  87.       endif
  88.    endif
  89.    if .not. file('&source')
  90.       clear
  91.       @ 10,15 say "File "+source+" not found! Try again!"
  92.       @ 23,0
  93.       wait
  94.       clear
  95.       source=space(12)
  96.       loop
  97.    endif
  98.    clear
  99.    @ 10,15 say "Name for file with new structure ?" get target
  100.    @ 12,15 say "(Default extension of .EXT supplied if none given.)"
  101.    read
  102.    r=readkey()
  103.    if r=12 .or. r=268
  104.       close data
  105.       return
  106.    endif
  107.    target=upper(ltrim(rtrim(target)))
  108.    if .not. "."$target
  109.       target=target+".EXT"
  110.    else
  111.       if right(target,1)="."
  112.          target=target+"EXT"
  113.       endif
  114.    endif
  115.    targetsdf=upper(ltrim(rtrim(substr(target,1,at(".",target)-1)))+".sdf")
  116.    clear
  117.    @ 10,15 say "Capturing structure to "+target+"..."
  118.    use &source
  119.    if file('&target')
  120.       erase &target
  121.    endif
  122.    copy to &target stru exte
  123.    use &target
  124.    clear
  125.    wait "Press any key to view current structure of "+target+"..."
  126.    ?
  127.    ?
  128.    disp all
  129.    wait "Press any key to copy this structure to text file for edit..."
  130.    clear
  131.    @ 10,15 say "Copying structure to text file "+targetsdf+" and loading editor..."
  132.    if file('&targetsdf')
  133.       erase &targetsdf
  134.    endif
  135.    copy to &targetsdf sdf
  136.    modi comm &targetsdf
  137.    clear
  138.    loadnew=.T.
  139.    @ 10,15 say "Load editted values for structure into "+target+" (Y/N)?" get loadnew
  140.    read
  141.    r=readkey()
  142.    if r=12 .or. r=268
  143.       close data
  144.       return
  145.    endif
  146.    if loadnew
  147.       zap
  148.       appe from &targetsdf sdf
  149.       clear
  150.       wait "Press any key to view new structure in "+target+"."
  151.       ?
  152.       ?
  153.       disp all
  154.       ?
  155.       wait
  156.       clear
  157.       changeit=.T.
  158.       @ 10,15 say "Append from original source into new structure? (Y/N)" get changeit
  159.       read
  160.       r=readkey()
  161.       if r=12 .or. r=268
  162.          close data
  163.          return
  164.       endif
  165.    endif
  166.    if .not. changeit
  167.       clear
  168.       ? source+" is left unaltered and as it was found by this program."
  169.       ?
  170.       ? target+" now holds the new structure for "+source+" as a dBase file."
  171.       ?
  172.       ? targetsdf+" holds the structure as an undelimited text file."
  173.       ? 
  174.       ? "You may now use "+target+" to create a new "+source+" with the"
  175.       ? "altered structure represented in "+target+" by the following"
  176.       ? "commands from the dBase dot prompt:"
  177.       ? 
  178.       ? "       . rename "+source+" to <new filename for &source.>"
  179.       ? "       . create "+source+" from "+target
  180.       ? "       . use "+source
  181.       ? "       . appe from <new filename for &source.>"
  182.       ? 
  183.       ? "You could also re-edit the text file "+targetsdf+" in order to further"
  184.       ? "modify its structure.  You would then take any file created with the"
  185.       ? "COPY STRUCTURE EXTENDED command, zap it, and:"
  186.       ?
  187.       ? "       . appe from "+targetsdf+" sdf"
  188.       ?
  189.       ? "Then use the series of commands above to load the new structure with"
  190.       ? "the data in the file with the former structure."
  191.       wait
  192.       clear
  193.    else
  194.       clear
  195.       oldsource=stuff(source,at(".",source)+1,3,"OLD")
  196.       oldsource=upper(oldsource)
  197.       if file('&oldsource')
  198.          erase &oldsource
  199.       endif
  200.       rename &source. to &oldsource.
  201.       @ 10,15 say "Renaming "+source+" to "+oldsource+", creating new"
  202.       @ 11,15 say "file "+source+" from "+target+" structure, appending"
  203.       @ 12,15 say "data values from "+oldsource+" to new "+source+"."
  204.       create &source from &target
  205.       use &source
  206.       appe from &oldsource
  207.       clear
  208.       ?
  209.       ? "The original source file is now saved as "+oldsource+"."
  210.       ?
  211.       ? "The newly structured file is saved as "+source+", with data values"
  212.       ? "appended from "+oldsource+"."
  213.       ?
  214.       ? "The file "+target+" holds the new structure."
  215.       ?
  216.       ? "The file "+targetsdf+" holds the new structure as an undelimited text file."
  217.       ?
  218.       ?
  219.       ?
  220.       ?
  221.       wait
  222.    endif
  223.    clear 
  224.    @ 10,15 say "Quit (Y/N)?" get quitter
  225.    read
  226.    r=readkey()
  227.    if r=12 .or. r=268
  228.       close data
  229.       return
  230.    endif
  231. enddo
  232. close data
  233. clear
  234. return
  235.