Rob van der Woude's Scripting Pages

Solutions found in alt.msdos.batch

and alt.msdos.batch.nt

Obtaining user input in NT

Walter Zackery posted two interesting solutions to obtain user input in NT:

re-posting for the world:
----------------



> W.Z., would you post your 2 methods here as well?
>
>

Sure, no problem. These are basically just copies of my postings to the
alt.msdos.batch group, with a couple of added comments.

METHOD #1:

Here is another method of obtaining user input in Windows NT. This one
uses the FORMAT command. Anyone that's nervous about using the FORMAT
command in a batch file can use my other posted method, which uses the
LABEL command. Personally, I prefer this method simply because it has a
cleaner input screen. This usage of the FORMAT command is absolutely
safe. There is certainly no chance that your hard drive will be
formatted. The command line that's used to obtain input is:

format/f:160 a: > %temp%\#input#

Note that the drive to be "formatted" is drive A. It doesn't really
matter whether there's actually a floppy in the drive or not, the batch
file will function just the same. If a floppy does happen to be in the
drive, NT will refuse to format it because the parameter passed to the
FORMAT command calls for a 160K floppy. I doubt seriously if there's
any NT user still using 160K floppies and I doubt even more seriously
if a 160K floppy would work at all in a standard floppy drive, assuming
that it could even fit. So the only danger here is to a user using a
non-protected 160K floppy in drive A at the time that this batch file
is run.

Note that for this may not work as written for non-English users
because the tokens in the FOR command may be incorrect. An up-or-down
adjustment of the tokens number should resolve this problem. The user
input is stored in the environmental variable %INPUT%.



REM Obtaining user input in Windows NT
REM with the use of the FORMAT command
REM posted August 20,1999
REM Author: Walter Zackery
@ECHO OFF & (set input=) & cls & echo\
echo Enter your input string and press ENTER when done.
echo\ & format/f:160 a: > %temp%\#input#

for /f "tokens=6*" %%a in (
'findstr \... %temp%\#input#') do if not "%%b"=="" (
set input=%%a %%b) else (set input=%%a)

set input & del %temp%\#input#

---------------------------------------------------------


METHOD #2:

The Windows NT LABEL command can be used to obtain user input. If you
type the command LABEL C: at the NT prompt, NT will prompt you for a
new label. Whatever you enter at that point can be redirected to a file
and then easily placed into the environment using standard NT
techniques. The input string is placed into the environment as the
variable %INPUT%, which the batch file will show to you. The first FOR
command saves your original label for drive C as a variable. This
variable is then restored later on, so that your original volume label
is retained.

A drawback about this procedure that I didn't catch at the time that I
originally posted this is that the maximum input string is limited to
32 characters. Any input string longer than this will generate an error
message and will NOT be saved to the environment. The reason for this
limitation on input is that the maximum length of a drive label in NT
happens to be 32 characters. The procedure posted above has no such
limitation so it is the preferred procedure.



REM Obtaining user input in Windows NT
REM with the use of the LABEL command
REM posted August 20,1999
REM Author: Walter Zackery
@ECHO OFF & cls & echo\ & (set oldlabel=)

for /f "tokens=6*" %%a in ('vol c:') do (
if not defined oldlabel set oldlabel=%%a %%b)

(set input=) & label c: > %temp%\$input$

for /f "tokens=*" %%? in (%temp%\$input$) do (set input=%%?)
set input & label c: %oldlabel%
del %temp%\$input$ & (set oldlabel=)



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 

The FORMAT trick is broken in Windows XP, as pointed out by Geoff Stafford -- thanks.
This could be solved by using Windows 2000's FORMAT executable.
But then again, there is no need to use dirty tricks like these in Windows 2000 or XP: just use SET /P instead.

 

More info on user input on my User Input page.


page last uploaded: 2015-08-24, 21:09