User Tools

Site Tools


How do I include a file without stating the whole file name?

Q: I would like to ask you if there is a way to include a file by just stating a part of its name. For example, I want to include test1.txt and I'm wondering if instead of stating:

$include test1.txt

I could use something like

$include test?.txt

GAMS does not allow you to specify wild cards in filenames. There is however a way around it using the wild cards of the shell:

$echon '$setglobal fname '          > setfname.gms
$if     %system.filesys% == UNIX $call ls test?.txt     | head -n 1 >> setfname.gms
$if not %system.filesys% == UNIX $call dir /B test?.txt | head -n 1 >> setfname.gms

$include setfname
$log *** including %fname%
$include %fname% 

Here dir (Windows) or ls (Unix) is used to produce a list of files that match your file pattern. Since there could be multiple files, we use the Posix utility head to get the first file in the list. The program head is part of any Unix OS and is included in the GAMS Windows system (subdirectory gbin) with recent GAMS distributions, so this little program should run on all platforms.

gams/include_a_file_without_stating_the_whole_file_name.txt · Last modified: 2007/09/26 15:46 (external edit)