如何在一段时间后退出SET / p命令?例如,如果用户输入的内容太长,我想退出SET / p。
答案 0 :(得分:4)
这是一个纯粹的批处理解决方案,可以避免使用Sendkeys时原始窗口未对焦时Batch-JScript混合脚本所具有的问题。
@echo off
setlocal EnableDelayedExpansion
rem Execute a SET /P command with time out
rem Antonio Perez Ayala
rem If this file is re-executed as pipe's right side, go to it
if "%~1" equ "TimeoutMonitor" goto %1
del InputLine.txt 2> NUL
(
set /P "line=You have 10 seconds to complete this input: " > CON
> InputLine.txt call set /P "=%%line%%" < NUL
) 2> NUL | "%~F0" TimeoutMonitor 10
set /P "line=" < InputLine.txt
del InputLine.txt
echo Line read: "!line!"
goto :EOF
:TimeoutMonitor
rem Get the PID of pipe's left side
tasklist /FI "IMAGENAME eq cmd.exe" /FO TABLE /NH > tasklist.txt
for /F "tokens=2" %%a in (tasklist.txt) do (
set "leftSidePipePID=!lastButOnePID!"
set "lastButOnePID=%%a"
)
del tasklist.txt
rem Wait for the input line, or until the number of seconds passed
for /L %%i in (1,1,%2) do (
ping -n 2 localhost > NUL
if exist InputLine.txt exit /B
)
rem Timed out: kill the SET /P process and create a standard input line
taskkill /PID %leftSidePipePID% /F > NUL
echo/
echo Timed Out> InputLine.txt
exit /B
答案 1 :(得分:2)
基本上,你不能。
您可以使用choice
接受超时的单个字符。
答案 2 :(得分:0)
下面是一个Batch-JScript混合脚本,可以实现您的目标。从XP开始,每个版本的Windows都安装了JScript。使用.bat扩展名保存此文件:
@if (@CodeSection == @Batch) @then
@echo off
rem Give the desired wait time in milliseconds:
start "" /B Cscript //nologo //E:JScript "%~F0" 10000
set "input=Time out"
set /P "input=You have 10 seconds to complete this input: "
echo Input read: %input%
goto :EOF
@end
WScript.Sleep(WScript.Arguments(0));
WScript.CreateObject("WScript.Shell").SendKeys("{ENTER}");
答案 3 :(得分:0)
如果允许使用可下载工具,则可以使用带有-t
参数的editv64.exe(64位)或editv32.exe(32位)。例如:
editv64 -p "Enter something within 10 seconds: " -t 10 INP
该工具还有其他优点,例如在键入(-m
)时隐藏输入字符串以及以交互方式编辑现有环境变量。你可以在这里下载: