如何在批处理文件中退出 SET/p [英] How to exit a SET /p in a batch file

查看:39
本文介绍了如何在批处理文件中退出 SET/p的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一段时间后退出 SET/p 命令?例如,如果用户输入的时间太长,我想退出 SET/p.

解决方案

这是一个纯 Batch 解决方案,避免了 Batch-JScript 混合脚本在使用 Sendkeys 时出现的问题,而原始窗口没有焦点.

>

@echo offsetlocal EnableDelayedExpansionrem 超时执行 SET/P 命令安东尼奥·佩雷斯·阿亚拉rem 如果这个文件作为管道的右侧重新执行,则转到它如果 "%~1" 等于 "TimeoutMonitor" 转到 %1del InputLine.txt 2>零(set/P "line=您有 10 秒的时间来完成此输入:" >康>InputLine.txt 调用集/P "=%%line%%" <零) 2>无 |"%~F0" 超时监视器 10设置/P "line=" <输入线.txt删除输入行.txt回声行读:!行!"转到:EOF:超时监视器rem 获取管道左侧的PID任务列表/FI "IMAGENAME eq cmd.exe"/FO TABLE/NH >任务列表.txtfor/F "tokens=2" %%a in (tasklist.txt) do (设置leftSidePipePID=!lastButOnePID!"设置lastButOnePID=%%a")删除任务列表.txtrem 等待输入行,或者直到经过的秒数for/L %%i in (1,1,%2) 做 (ping -n 2 本地主机 >零如果存在 InputLine.txt 退出/B)rem 超时:终止 SET/P 进程并创建标准输入行taskkill/PID %leftSidePipePID%/F >零回声/回声超时>输入线.txt退出/B

How can I exit a SET /p command after a certain time? For instance, if a user takes too long to input something, I want to exit the SET /p.

解决方案

This is a pure Batch solution that avoid the problems that the Batch-JScript hybrid script have when Sendkeys is used and the original window is not in focus.

@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

这篇关于如何在批处理文件中退出 SET/p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆