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

J

jjm

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?
 
T

Tom Lavedas

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?

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
 
1

127.0.0.1

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?

Try choice.(exe|com)
 
T

Timo Salmi

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?

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
 
B

Bill Stewart

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top