[Toc][Index]

Automatic Batch Files


CMD.EXE supports "automatic" batch files, files that run without your 
intervention, as long as the command processor can find them. 
Each time CMD.EXE starts as either a primary or a secondary shell, it 
looks for an automatic batch file called 4START.BTM or 4START.CMD.  If the 
4START batch file is not in the same directory as CMD.EXE itself, you 
should use the Startup page of the OPTION dialogs or the 4StartPath 
directive in your .INI file to specify its location.  4START is optional, 
so CMD.EXE will not display an error message if it cannot find the file. 
4START is a convenient place to change the color or content of the prompt 
for each shell, LOG the start of a shell, or put other special startup or 
configuration commands.  4START is also a good place to set aliases and 
environment variables. 
The entire startup command line passed to the command processor is 
available to 4START via batch file parameters (%1, %2, etc.).  This can be 
useful if you want to see the command line passed to a secondary shell by 
an application.  For example, to pause if any parameters are passed to a 
secondary shell you could include this command in 4START (enter this on 
one line): 


        if "%1" != "" .and. "%_shell" gt 0 pause Starting shell
        %_shell with parameters [%$]

Whenever a CMD.EXE shell ends, it runs an automatic batch file called 
4EXIT.BTM, 4EXIT.CMD.  This file, if you use it, should be in the same 
directory as your 4START batch file.  Like 4START, 4EXIT is optional.  It 
is not necessary in most circumstances, but it is a convenient place to 
put commands to save information such as a history list before a shell 
ends, or LOG the end of the shell. 

Pipes, Transient Sessions, and 4START 
When you set up the 4START file, remember that it is executed every time 
CMD.EXE starts, including when running a pipe, or a transient copy of the 
command processor started with the /C startup option. 
For example, suppose you enter a command line like this, which uses a 
pipe: 


        [c:\data] myprog | sort > out.txt

Normally this command would create the output file C:\DATA\OUT.TXT. 
 However, if you have a 4START file which changes to a different 
directory, the output file will be written there -- not in C:\DATA. 
This is because the command processor starts a second copy of itself to 
run the commands on the right hand side of the pipe, and that new copy 
runs 4START before processing the commands from the pipe.  If 4START 
changes directories, the command from the pipe will be executed in the new 
directory. 
The same problem can occur if you use a transient session started with /C 
to run an individual command, then exit -- the session will execute in the 
directory set by 4START, not the directory in which it was originally 
started.  For example, suppose you set up an OS/2 desktop object with a 
command line like this, which starts a transient session: 


        Command:            d:\CMD.EXE\CMD.EXE.exe /c list myfile.txt
        Working Directory:  c:\data

Normally this command would LIST the file C:\DATA\MYFILE.TXT.  However, if 
4START changes to a different directory, the command processor will look 
for MYFILE.TXT there -- not in C:\DATA. 
Similarly, any changes to environment variables or other settings in 
4START will affect all copies of the command processor, including those 
used for pipes and transient sessions. 
You can work around these potential problems with the IF or IFF command 
and the internal variables _PIPE and _TRANSIENT.  For example, to skip all 
4START processing when running in a pipe or transient session, you could 
use a command like this at the beginning of 4START: 


        if %_pipe != 0 .or. %_transient != 0 quit


Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs