Friday, May 16, 2008

GAMS put file names with date/time attached

A question on the GAMS list was:


>I would like to write the results of a GAMS program into a text file
>whose name should depend on the date and time of the run. How could
>that be done?
>
>Ex: output-05.15.08-09.39.txt
>The above is just a sample and it does not have to be exactly the same.

The suggestion to use myfile /filename_%system.date%_%system.time%/ does not work for several reasons: quotes are needed to handle embedded blanks and the ‘/’ characters in the system date cause problems when used inside a file name. Here is a different approach that actually works (I ran it before suggesting it):

$onecho > createfile.awk
BEGIN { print "file f / \"f_" strftime("%Y-%m-%d_%H.%M.%S") ".txt\"/" }
$offecho
 
$call "awk -f createfile.awk > ftime.inc"
 
$include ftime.inc
 
put f "hello"/;
The generated include file will look like:
file f / "f_2008-05-16_14.28.59.txt"/
For more info on AWK see gawk.html