0

im new to batch scripting. I was wondering if there was a way to write a timed prompt without using choice.exe because I'm using windows xp. Something along the lines of:

@echo off
/set p answer=input y or n in 30 seconds
rem start timer
if answer==y(
  goto :action1
)
if answer==n(
  goto action2
)
rem if timer has expired without input go to :action1

:action 1
echo you have entered y
:action 2
echo you have entered n

Thank you for the hints.

stewazy
  • 139
  • 1
  • 3
  • 8

2 Answers2

0

Here's one idea. You could use Wscript.Shell's Popup() method. It includes a timeout parameter. A Yes / No dialog will return 6 on Yes, 7 on No, and -1 on timeout. This is easy enough to convert to Boolean as true on Yes / timeout, false on No. Save this with a .bat extension and you'll see how it works.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set delay=30
set "msg=Do you wish to continue?"

cscript /nologo /e:JScript "%~f0" "%msg%" %delay% && (
    goto yes
) || (
    goto no
)

:yes
echo You chose yes.
exit /b

:no
echo You chose no.
exit /b

@end // end Batch / begin JScript hybrid code

var osh = WSH.CreateObject('WScript.Shell'),
    msg = WSH.Arguments(0),
    secs = WSH.Arguments(1);

var response = osh.Popup(msg, secs, 'Waiting for response', 4 + 32);
// yes or timeout returns true; no returns false
WSH.Quit(!(response - 7));
rojo
  • 24,000
  • 5
  • 55
  • 101
0

You can try this:

EditVar and Choose

Choose is similar to the Microsoft Choice tool, but it has more features. Here are some reasons why it might be preferable to Choice:

  • It doesn't beep when the user makes an invalid choice.

  • It offers a "default key" feature, which lets a user press Enter to select a default choice.

  • It comes with a real-mode DOS version (useful for MS-DOS boot media). The Win32 version's timeout feature doesn't get confused when you run multiple instances in separate console windows (this was a problem with earlier Win32 console versions of Microsoft's Choice tool).

  • A 64-bit version is provided.

  • It can suppress the display of the user's choice.

  • It offers a "line input" mode where the user must press Enter after making a > choice.

This is an example:

CHOOOSE64.exe -c YNA -d A -n -p "Prompt" -t A,30
rem           -Choices
rem                  -Default
rem                       -No default prompt
rem                          -Prompt(custom)
rem                                     -timeout defaultChoice, timeout in seconds

if %errorlevel%==1 echo Y
if %errorlevel%==2 echo N
if %errorlevel%==3 echo A or timeout-ed
Community
  • 1
  • 1