Batch file 如何在批处理文件中退出集合/p
在特定时间后,如何退出SET/p命令?例如,如果用户输入内容花费的时间太长,我想退出集合。基本上,您不能Batch file 如何在批处理文件中退出集合/p,batch-file,cmd,Batch File,Cmd,在特定时间后,如何退出SET/p命令?例如,如果用户输入内容花费的时间太长,我想退出集合。基本上,您不能 您可以使用choice来接受带有超时的单个字符。下面有一个批处理JScript混合脚本,可以实现您想要的功能。JScript随XP上的每个Windows版本一起安装。以.bat扩展名保存此文件: @if (@CodeSection == @Batch) @then @echo off rem Give the desired wait time in milliseconds: star
您可以使用
choice
来接受带有超时的单个字符。下面有一个批处理JScript混合脚本,可以实现您想要的功能。JScript随XP上的每个Windows版本一起安装。以.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}");
如果允许使用可下载的工具,则可以将editv64.exe(64位)或editv32.exe(32位)与
-t
参数一起使用。例如:
editv64 -p "Enter something within 10 seconds: " -t 10 INP
该工具还有其他优点,例如在键入(-m
)时隐藏输入字符串,以及以交互方式编辑现有环境变量。你可以在这里下载:
这是一个纯粹的批处理解决方案,可以避免使用Sendkeys时批处理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
@echo关闭
setlocal EnableDelayedExpansion
rem在超时情况下执行SET/P命令
雷姆·安东尼奥·佩雷斯·阿亚拉
rem如果此文件作为管道右侧重新执行,请转到它
如果“%$1”equ“超时监视器”转到%1
del InputLine.txt 2>NUL
(
set/P“line=您有10秒时间完成此输入:”>CON
>InputLine.txt调用集/P“=%%line%%”NUL |“%~F0”超时监视器10
设置/P“line=“tasklist.txt
对于(tasklist.txt)中的/F“tokens=2”%%a,请执行以下操作(
设置“leftSidePipePID=!lastButOnePID!”
设置“lastButOnePID=%%a”
)
del tasklist.txt
rem等待输入行,或直到超过秒数
对于(1,1,%2)中的/L%%i,请执行以下操作(
ping-n2 localhost>NUL
如果存在InputLine.txt退出/B
)
rem超时:终止SET/P进程并创建标准输入行
taskkill/PID%leftSidePipePID%/F>numl
回音/
echo超时>InputLine.txt
退出/B
当SendKeys方法发送Enter键击时,如果焦点在控制台窗口上,此解决方案才“有效”。因此,这不是一个真正健壮的解决方案。@Bill_Stewart这可能是您能得到的最好的解决方案。@JohnDOE-IMO editv32/editv64是一个更好、更健壮的解决方案。“如果允许下载工具”,这是您自己的话@默认情况下,Aacini的答案有效。这就是为什么它是你能得到的“最好的”。它只有在焦点不变的情况下才有效。因此,在许多情况下,它是不可靠的。如果无法使用可下载工具,则最好使用choice
或timeout
命令。这取决于当前用户对当前目录具有RWXD权限。无法保证情况会是这样。@BILL在您的脚本上发出sed's/InputLine.txt/%TMP%\\InputLine.txt/g'并解决了这个问题,对吧,假设用户对%TMP%有写权限,否则用您希望的任何路径替换%TMP%,如果您使用sed搜索/替换,您必须用“\\”转义。另一个选项是在notepad.exe中打开文件并进行搜索和替换。搜索:InputLine.txt Replace:“\InputLine.txt这些是Aacini无法猜测的环境问题,通常很难解决;你必须承认,他做了最难的部分。