B  DOS batch files in Scheme

DOS shell scripts are known as batch files. A conventional DOS batch file that outputs “Hello, World!”​ ​has the following contents:

echo Hello, World! 

It uses the DOS command echo. The batch file is named hello.bat, which identifies it to the operating system as an executable. It may then be placed in one of the directories on the PATH environment variable. Thereafter, anytime one types


or simply


at the DOS prompt, one promptly gets the insufferable greeting.

A Scheme version of the hello batch file will perform the same output using Scheme, but we need something in the file to inform DOS that it needs to construe the commands in the file as Scheme, and not as its default batch language. The Scheme batch file, also called hello.bat, looks like:

;@echo off 
;goto :start 
echo. > c:\_temp.scm 
echo (load (find-executable-path "hello.bat" >> c:\_temp.scm 
echo "hello.bat")) >> c:\_temp.scm 
mzscheme -r c:\_temp.scm %1 %2 %3 %4 %5 %6 %7 %8 %9 
goto :eof 
(display "Hello, World!") 

The lines upto |# are standard DOS batch. Then follows the Scheme code for the greeting. Finally, there is one more standard DOS batch line, viz., ;:eof.

When the user types hello at the DOS prompt, DOS reads and runs the file hello.bat as a regular batch file. The first line, ;@echo off, turns off the echoing of the commands run — as we don’t want excessive verbiage clouding the effect of our script. The second line, ;goto :start, causes execution to jump forward to the line labeled :start, i.e., the fourth line. The three ensuing echo lines create a temporary Scheme file called c:\_temp.tmp with the following contents:

(load (find-executable-path "hello.bat" "hello.bat"))

The next batch command is a call to MzScheme. The ‑r option loads the Scheme file c:\_temp.scm. All the arguments (in this example, none) will be available to Scheme in the vector argv. This call to Scheme will evaluate our Scheme script, as we will see below. After Scheme returns, we still need to ensure that the batch file winds up cleanly. The next batch command is goto :eof, which causes control to skirt all the Scheme code and go to the very end of the file, which contains the label ;:eof. The script thus ends.

Now we can see how the call to Scheme does its part, viz., to run the Scheme expressions embedded in the batch file. Loading c:\_temp.scm will cause Scheme to deduce the full pathname of the file hello.bat (using find‑executable‑path), and to then load hello.bat.

Thus, the Scheme script file will now be run as a Scheme file, and the Scheme forms in the file will have access to the script’s original arguments via the vector argv.

Now, Scheme has to skirt the batch commands in the script. This is easily done because these batch commands are either prefixed with a semicolon or are enclosed in #| ... |#, making them Scheme comments.

The rest of the file is of course straight Scheme, and the expressions therein are evaluated in sequence. (The final expression, ;:eof, is a Scheme comment, and causes no harm.) After all the expressions have been evaluated, Scheme will exit.

In sum, typing hello at the DOS prompt will produce

Hello, World! 

and return you to the DOS prompt.