&ARGS selmask
&CALL CheckArgs
&TERMINAL 9999

/* This aml will convert all the DEM files in a directory to 
/* ArcInfo grids.  It must be run within GRID.  
/* Sample usage: dem2grid *.dem

/* To be perfectly honest, I swiped and adapted much of this code when
/* I needed to convert about 60 DEMs in a batch.  My thanks to everyone
/* who posts on ArcScripts!!!

/* Justin Mills, Nov. 2001
/* justin.mills@orst.edu

/* load DEM names into a scratch file
&SETVAR scratchfile := [SCRATCHNAME -PREFIX ~LST -FILE]
&SETVAR count := [FILELIST %selmask% %scratchfile% -FILE]  
&IF %count% <= 0 &THEN
  &RETURN Error generating grid list file.
&TYPE Converting %count% DEMs.

/* open list of file names
&SET dems := [OPEN %scratchfile% filestatus -READ]
&IF %filestatus% <> 0 &THEN
  &RETURN &WARNING Error %filestatus% opening scratch file.


/* begin processing DEMs
&DO I := 1 &TO [CALC %count% - 1]

  /* Read file name
  &SETVAR indem := [READ %dems% readstatus]

  /* Create grid name
  &SETVAR gridname := [BEFORE %indem% .]

  /* Convert to a grid
  DEMLATTICE %indem%  %gridname% USGS

 /* Display file name
 &TYPE Grid %gridname% created.


&END
/* Exit gracefully
&CALL Exit
&RETURN


/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::ROUTINES
/* CHECKARGS --------------------------------------------------------------
/* commandline parser
   &ROUTINE CheckArgs
     &IF [NULL %selmask%] &THEN &CALL Syntax
   &RETURN
 

/* SYNTAX -----------------------------------------------------------------
/* show syntax when there is an insufficent number of arguments.
   &ROUTINE Syntax
     &TYPE Usage: DEM2GRID <selection mask> 
     &RETURN &WARNING Done.
   &RETURN &WARNING
   
/* EXIT -------------------------------------------------------------------
/* reset user settings to those in effect when script called.
   &ROUTINE Exit
     &IF NOT [NULL %scratchfile%] &THEN
       &IF [EXISTS %scratchfile% -FILE] &THEN
         &IF [DELETE %scratchfile%] = 0 &THEN &TYPE Done. 
   &RETURN