Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

wait for user choice by pressing keyboard, select the default one if timeout

81 views
Skip to first unread message

jjm

unread,
Feb 11, 2011, 2:44:53 AM2/11/11
to
i'm writing a batch file to mimic a simple menu system

in my batch file i want to wait for user input for seconds, if user
doesn't press any key during this period then make the default choice.
i have googled for a while, what i know now is i could use ping to
achieve the delay function , but i don't know how to get the user
input during this period?

Tom Lavedas

unread,
Feb 11, 2011, 6:03:15 PM2/11/11
to

The closest I can think of is the old Choice.exe program, but it is no
longer supplied with later versions of windows. I think Win NT was
the last one that cam with it.

Or you can try my little home brew that uses a hybrid JScript/batch
approach ...

@set @x=0 /* Batch part
@echo off
setlocal
set "Input="&set delay=10000 %/ milliseconds /%
set /p Input=Enter data here: < nul
for /f "delims=" %%I in (
'cscript //nologo //e:jscript "%~f0" %delay%'
) do endlocal & set "Input=%%I"
if not defined input echo.
echo.For example: "%Input%"
goto :EOF

Jscript part */
var d=WSH.Arguments(0);var t=0;
with(new ActiveXObject("WScript.Shell")){
with(Exec('%comspec% /c(set /p _#=<con&set _#)')){
while(Status==0){
WSH.Sleep(50);t+=50;if(t>d)Terminate()};
if (!StdOut.AtEndofStream) {
WSH.Echo(StdOut.ReadLine().substr(3))}}}
_______________________
Tom Lavedas

billious

unread,
Feb 12, 2011, 1:05:32 AM2/12/11
to

Hmm. I believe that NT did not have choice.exe - it was included in 98 and
me.

This hybrid-scripting seems to be a viable approach here. I'd offer two
modifications

1/ add

(set @x=)

directly after the "@echo off" to clean up the environment a little

2/ if not defined input echo.
becomes

if not defined input set input=DEFAULTVALUE

to get closer to OP's ambition.

Not quite a complete solution though - as this requires a CR and CHOICE
worked on the single key-entry.

I'll continue this in your "hybrid scripting" thread in alt.msdos.batch.nt


127.0.0.1

unread,
Mar 14, 2011, 1:13:51 PM3/14/11
to

Try choice.(exe|com)

Timo Salmi

unread,
Mar 19, 2011, 1:21:41 AM3/19/11
to

If you can find a choice.com (or a similar program) use it with
/t[:]c,nn switch

See http://www.netikka.net/tsneti/info/tscmd014.htm

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Bill Stewart

unread,
Mar 28, 2011, 12:31:39 PM3/28/11
to

Hi,

You can also use editvar/editv32/editv64 or choose/choose32/choose64

editvar/choose - MS-DOS versions
editv32/choose32 - Windows console x86 versions
editv64/choose64 - Windows console x64 versions

editvar/editv32/editv64 is a utility that lets you interactively edit
environment variables. It has several features many have told me they
have found useful, such as timeout and masked input (displays '*' for
typed characters, useful for entering passwords).

choose/choose32/choose64 is an alternative to the Microsoft 'choice'
program and offers the same feature set with a couple of enhancements.

All programs are freeware, available from my web site:
http://www.westmesatech.com/editv.html

--
Bill Stewart

0 new messages