User Input

It is somewhere between extremely difficult and impossible to get user input of more than single characters into a batch program. CHOICE, where available and where it will serve, and utilities like those in my page on DEBUG scripts can deal with single characters, but there is no convenient line input utility readily available, and you can pretty well forget about block input and a number of characters that break the pure batch tricks.

Good old AWK to the rescue. AWK is line oriented to begin with, so a trivial script like
BEGIN{
	getline
	print $0
}
will echo whatever single line is typed on the keyboard to the STDOUT stream. Since STDOUT can be redirected and the script can easily be collapsed into a single line, we can make that echoing of the keyboard a one-liner:
gawk "BEGIN{getline;print $0}"
that can be used in a batch file to do things like get program names, file names, user names, ... whatever. See Multilingual Batch Programs for information about obtaining the free GAWK language and dealing with its documentation.

This example puts whatever the user types on a single line into an environment variable for whatever use you need to make of it.

ASUI0010.BAT(INPUT.BAT)
 @echo off
 echo Enter the string to be placed into the environment.
 gawk "BEGIN{getline;print \"set string=\"$0}" > }{.bat
 call }{
 del }{.bat
Note that additional text has been added in front of the typed string and the necessary quotes have been escaped with backslashes. That program leaves whatever string was typed in the STRING environment variable.

In some cases, the program can be simplified even farther: if the string is a program (even a batch file) to be executed, the whole thing becomes ASUI0020.BAT(INPUT.BAT)
 @echo off
 echo Enter the name of the program to run.
 gawk "BEGIN{getline;print "$0}" > }{.bat
 call }{
 del }{.bat


*********more later**********




  ** Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001 Ted Davis - see License, included by reference. ** 

Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.

Back to the Table of Contents page

Back to my personal links page - back to my home page