The following utility function generates a program file that will recreate all indexes from scratch. This generated program also documents the index tags. It uses the MDX( ) function to get the name of the .MDX file that contains the active index tag. If there is no active index tag, the production .MDX is used. Please note that this program does not take into account primary and distinct indexes.

PROCEDURE GenMDX( cFile )

   local cMdx, cMdxFile 

   cMdx = mdx( ) 

   if cMdx == "" 

      *-- If no active index tag, try production .MDX 

      cMdx = mdx( 1 ) 

      if cMdx == "" 

         msgbox( "No MDX file", "Nothing to do", 48 ) 

         return  

      endif 

   endif 

 

   *-- Set OF clause for non-production .MDX 

   cMdxFile = iif( cMdx == mdx( 1 ), "", [ of "] + cMdx + ["] ) 

   *-- Remove drive and/or path from .MDX filename 

   *-- (after setting OF clause, because that checks cMdx) 

   cMdx = substr( cMdx, max( rat( ":", cMdx ), rat( "\", cMdx ) ) + 1 ) 

   local lSafety 

   lSafety = ( set( "SAFETY" ) == "ON" ) 

   set safety on 

 

   if argcount( ) < 1 

      cFile = left( cMdx, rat( ".", cMdx ) - 1 ) + "X.PRG" 

   endif 

 

   set alternate to ( cFile ) 

   set console off 

   ? 

   set alternate on 

 

   ??"* " + cFile 

   ? "*" 

   ? "* Index file for " + cMdx 

   if cMdxFile == "" 

      ?? " (production .MDX)" 

   endif 

   ? "*" 

   ? "* Generated on " + dtoc( date( ) ) + " " + time( ) 

   ? "*" 

   ? 

   ? [*-- Delete all current tags from specific .MDX only] 

   ? [do while "" # tag( "] + cMdx + [", 1 )] 

   ? [ delete tag tag( "] + cMdx + [", 1 )] 

   ? [enddo] 

   ? 

 

   nNdx = 1 

   do while "" # key( cMdx, nNdx ) 

      ? [index tag ] + transform( tag( cMdx, nNdx ), "@! XXXXXXXXXX" ) + ; 

      cMdxFile + [ on ] + key( cMdx, nNdx ) 

      if "" # for( cMdx, nNdx ) 

         ?? [ for ] + for( cMdx, nNdx ) 

      endif 

      if descending( cMdx, nNdx ) 

         ?? [ descending] 

      endif 

      if unique( cMdx, nNdx ) 

         ?? [ unique] 

      endif 

      nNdx = nNdx + 1 

   enddo 

 

   close alternate 

   if .not. lSafety 

      set safety off 

   endif 

The function uses the KEY( ), FOR( ), DESCENDING( ), and UNIQUE( ) functions to get the definition of each index.