0

我是批处理脚本的新手。我想知道是否有办法在不使用choice.exe 的情况下编写定时提示,因为我使用的是windows xp。类似于以下内容:

@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

谢谢你的提示。

4

2 回答 2

0

你可以试试这个:

EditVar 和选择

选择类似于 Microsoft 选择工具,但它具有更多功能。以下是它可能比 Choice 更可取的一些原因:

  • 当用户做出无效选择时,它不会发出哔哔声。

  • 它提供“默认键”功能,让用户按 Enter 键选择默认选项。

  • 它带有实模式 DOS 版本(对 MS-DOS 启动媒体有用)。当您在单独的控制台窗口中运行多个实例时,Win32 版本的超时功能不会混淆(这是 Microsoft's Choice 工具的早期 Win32 控制台版本的问题)。

  • 提供 64 位版本。

  • 它可以抑制用户选择的显示。

  • 它提供了一种“行输入”模式,用户必须在做出 > 选择后按 Enter 键。

这是一个例子:

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
于 2017-05-09T09:33:27.673 回答
0

这是一个想法。您可以使用Wscript.Shell'Popup()方法。它包括一个超时参数。是/否对话框将在是时返回 6,在否时​​返回 7,在超时时返回 -1。这很容易转换为布尔值,true如是/超时,false否。用 .bat 扩展名保存它,你会看到它是如何工作的。

@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));
于 2017-05-09T02:19:54.623 回答